Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Regexp substitution using variables

by davido (Cardinal)
on Nov 25, 2020 at 22:55 UTC ( [id://11124237]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Regexp substitution using variables
in thread Regexp substitution using variables

This is interesting. Can you provide some additional examples, including more esoteric ones, and possible a little sample text? I was just wanting to look at the challenges you're facing more pragmatically. Test cases would be fantastic.


Dave

Replies are listed 'Best First'.
Re^4: Regexp substitution using variables
by MikeTaylor (Acolyte) on Nov 25, 2020 at 23:23 UTC
    I'm afraid I'm not yet far enough into the project to have solid examples, let alone test cases. I am waiting on the customer to let me know what specific transformations they need. But it would not be unlikely that we'd find, for example, a field containing call-numbers like PR.123.ABC that we needed to change to PR-ABC:123, which of course we could do with s/(.*)\.(.*)\.(.*)/$1-$3:$2/.

      Win8 Strawberry 5.8.9.5 (32) Thu 11/26/2020 5:05:35 C:\@Work\Perl\monks >perl use strict; use warnings; my $pattern = '(.*)\.(.*)\.(.*)'; my $replacement = '$1-$3:$2'; my $flags = ''; # $got_g is true if /g modifier present in flags. # ($flags, my $got_g) = sanitize_flags_detect_g($flags); fixup_forward_slashes($pattern, $replacement); my $value = 'PR.123.ABC'; print "replacement '$replacement' \n"; my $eval_string = "\$value =~ s/$pattern/$replacement/$flags"; print "eval_string '$eval_string' \n"; eval $eval_string; print "eval err '$@' \n"; print "output '$value' \n"; sub fixup_forward_slashes { s{/}'\/'g for @_; } ^Z replacement '$1-$3:$2' eval_string '$value =~ s/(.*)\.(.*)\.(.*)/$1-$3:$2/' eval err '' output 'PR-ABC:123'


      Give a man a fish:  <%-{-{-{-<

        And, as LanX showed in Re^3: Regexp substitution using variables, you can now try
        $replacement = '@{[`ls`]}; # use dir on MSWin
        or even rm -rf / or deltree or whatever the equivalent is. This is too strong.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124237]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-03-28 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found