Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Yet another regex question

by gregor-e (Beadle)
on Aug 25, 2005 at 19:44 UTC ( [id://486712]=note: print w/replies, xml ) Need Help??


in reply to Yet another regex question

So I hear two votes for using a proper parser and several piecewise suggestions that, unfortunately, don't lend themselves to transforming the entire file in one substitute.

I was hoping there might be some trick like
 $programSource =~ s/in \(('([^']*)'[,])*\)/=~ m#[$2$3$4$5$6$7$8$9]#;/g;
where perhaps the outer group of parentheses would be $1 and any number of nested inner parentheses groups would be matched for all patterns matching ([^']*) inside the single-quotes. In a more broken-out fashion:

$programSource =~ s/in \( (' <-- outer group start goes in $1 ([^']*) <-- inner group chars between '' '[,])* <-- followed by optional comma, close outer group + and repeat outer group for any number of repetitions \)/=~ m#[$2$3$4$5$6$7$8$9]#;/g; <-- put inner grou +ps in match (up to 8 of them, anyway)

Since I'm trying to use global substitutes that can span several lines and update the entire source file in one substitutution, and since there are only three cases in the source I am currently trying to transform, I'll probably just use three separate global substitutes, one for each of the in ('X'), in ('X','Y') and in ('X','Y','Z') cases. It just seems like it ought to be something one could do with a single fancy global substitute.

Thanks for your thoughts.

Replies are listed 'Best First'.
Re^2: Yet another regex question
by QM (Parson) on Aug 26, 2005 at 22:07 UTC
    The problem is one of picking out pieces. A "normal" s/// won't work, but a cheeky (?{code block}) does:
    #!/your/perl/here use strict; use warnings; my $one = q/in ('X')/; my $two = q/in ('X','Y')/; my $three = q/in ('X','Y','Z')/; foreach my $s ( $one, $two, $three ) { our $r=''; (my $u = $s) =~ s{ # open regex - curly delimiters in\s+ \( # literal open paren (?: # non-capturing group ' # literal single quote ( # capture group [^'] # single non-single-quote-char ) # close capture group (?{$r=$r.$^N}) ' # literal single quote ) # closing non-capture group (?: # opening non-capture group ,' # literals ( # capture group [^'] # single non-single-quote-char ) # close capture group (?{$r=$r.$^N}) ' # literal single-quote )* # close non-capture group \) # literal close paren } # close regex - curly delimiter {=~\ m/\[$r\]}x; print "$s\n$u\n\n"; }
    But that's not usually a good idea.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

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

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

    No recent polls found