Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Match a pattern only if it is not within another pattern

by pbeckingham (Parson)
on Aug 16, 2005 at 19:48 UTC ( #484226=note: print w/ replies, xml ) Need Help??


in reply to Match a pattern only if it is not within another pattern

Use negative look-behind and negative lookahead.

$str =~ s/((?<!bar)\S*)foo(\S*(?!qux))/${1}123${2}/gx;
Update: Yup - it's broke. Nothing to see here, move along.



pbeckingham - typist, perishable vertebrate.


Comment on Re: Match a pattern only if it is not within another pattern
Download Code
Re^2: Match a pattern only if it is not within another pattern
by Transient (Hermit) on Aug 16, 2005 at 19:51 UTC
    That gave me
    bl123 and barthis123thatqux and barso123quxhim and123som 123


    Update:
    However this seems to do what is intended, at least with the test string:
    $str =~ s/((?<!bar)\S*)foo((?!\S*qux))/${1}123${2}/gx;
    gives
    bl123 and barthisfoothatqux and barsofooquxhim and123som 123
Re^2: Match a pattern only if it is not within another pattern
by punkish (Priest) on Aug 16, 2005 at 19:53 UTC
    sorry, this doesn't work...
    $str =~ s/((?<!bar)\S*)foo(\S*(?!qux))/${1}123${2}/gx; print "$str\n";

    prints

    bl123 and barthis123thatqux and barso123quxhim and123som 123
    --

    when small people start casting long shadows, it is time to go to bed

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (18)
As of 2015-07-02 13:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (38 votes), past polls