Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Appending strings

by johngg (Canon)
on Mar 11, 2016 at 22:46 UTC ( [id://1157506]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Appending strings
in thread Appending strings

If I have understood correctly what you require, use the "word boundary" asserstion (\b), see perlre.

use strict; use warnings; use 5.014; my $path = q{XX.YY.ZZ}; my $str = q{(ABC | (~dEf & hI_J & endxxx)) & ~LMn if ~xYz end}; # Make a compiled regex of an alternation of the terms to exclude. # my $rxExcl = do { my @excl = qw{ if else 1'b0 end }; local $" = q{|}; qr{(?x) (?: @excl ) \b}; }; my @posns; push @posns, pos $str while $str =~ m{(?x) # Use extended regex syntax (?<! [A-Za-z_] ) # Match at a point not preceded by # letters or underscore (?= [A-Za-z_] ) # And which is followed by letters # or underscore (?! $rxExcl ) # But not followed by any our terms # to exclude }g; substr $str, $_, 0, qq{$path.} for reverse @posns; say $str;

The output.

(XX.YY.ZZ.ABC | (~XX.YY.ZZ.dEf & XX.YY.ZZ.hI_J & XX.YY.ZZ.endxxx)) & ~ +XX.YY.ZZ.LMn if ~XX.YY.ZZ.xYz end

I hope this is what you want.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^4: Appending strings
by doofus (Acolyte) on Mar 11, 2016 at 23:50 UTC
    Thanks John, that works, you're awesome! By the way, what does this line do? qr{(?:@excl)};
      ... what does this line do? qr{(?:@excl)};

      That statement, which by the way is really
          qr{(?x) (?: @excl ) \b};
      interpolates the strings that have been assigned to the  @excl array into a pre-compiled  qr// "regex object" (see Regexp Quote-Like Operators). The interpolation is under the control of the  $" special variable (see perlvar), which has been assigned the  '|' string, which is the regex alternation operator. The full effect of the block in which this statement executes can easily be seen:

      c:\@Work\Perl>perl -wMstrict -le "my $rxExcl = do { my @excl = qw{ if else 1'b0 end }; local $\" = q{^|}; qr{(?x) (?: @excl ) \b}; }; print $rxExcl; " (?^:(?x) (?: if|else|1'b0|end ) \b)


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-18 13:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found