Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: perl regexp question excluding strings

by AnomalousMonk (Archbishop)
on Nov 05, 2011 at 14:10 UTC ( [id://936151]=note: print w/replies, xml ) Need Help??


in reply to perl regexp question excluding strings

Further to GrandFather's wise advice on regex factoring, here's another approach.

The idea is that the requirement to "not match ... if ... a certain server name or a certain string comes before Servername" suggests a negative look-behind assertion. The problem with this is the inference I make that the look-behind could be variable-width, and such a look-behind is not directly supported in Perl 5. (I am perhaps improperly reading into the above quotation the possibility that there could be two or more strings of differing lengths to be excluded.)

A variable-width negative look-behind can be emulated. The trick is that a positive match on one of several completely arbitrary patterns causes a substring needed in a subsequent match to be 'consumed', and then the whole sub-match is 'failed', preventing the subsequent match from succeeding. See  (*SKIP) and  (*FAIL) (only available in 5.10+) in the Special Backtracking Control Verbs section of perlre.

>perl -wMstrict -le "my @strs = ( 'Servername FOO', 'fine Servername FOO yyy', 'no Servername FOO zzz', 'cool Servername BAR', 'nyet Servername BAR vvv', 'Servername BOFF', 'ok Servername BOFF yyy', 'no Servername BOFF zzz', ); ;; my @names = qw(FOO BAR); my @avoid = qw(no nyet); ;; my $find = join '|', @names; $find = qr{ Servername \s+ (?: $find) }xms; ;; my $not_before = join '|', @avoid; $not_before = qr{ (?: $not_before) \s+ Servername }xms; $not_before = qr{ (?: $not_before (*SKIP)(*FAIL))? }xms; ;; for my $s (@strs) { print qq{'$s'} if $s =~ m{ $not_before $find }xms; } " 'Servername FOO' 'fine Servername FOO yyy' 'cool Servername BAR'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-24 03:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found