Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Writing a negative regex using =~

by ftumsh (Scribe)
on Oct 17, 2012 at 09:36 UTC ( #999506=perlquestion: print w/ replies, xml ) Need Help??
ftumsh has asked for the wisdom of the Perl Monks concerning the following question:

Lo,

I am trying to implement a -ve regex using only the =~ construct. ie The regex should match if a string is not found in the source string. I am using a daemon that accepts regexes and applies them to files, which is why I am stuck with the =~ construct.

Here is a sample string:

L,0010,8001149805,003,20121015,0600,267,204193,20121015,1300,,GBFER,204193

I should like to have a positive match if the string doesn't contain GBFER and GBFOO. Just like !~ /(?:GBFER|GBFOO)/.

All I can know about the string is that it starts with "L," and has a fixed number of commas. Everything between the commas may be zero or multiple alphanumeric. Also, the bit I am wanting to test will be between the 11th and 12th commas.

I did have the (as I thought hours ago) wizzo idea of having a conditional regex with a look ahead assertion. If the lookahead matched, I'd use a regex that always failed. If the look ahead didn't match, I'd use a regex that always matched.

However, after hours of google and RTFM, I can't get it to work (it is another node)...

So, any and all comments, ideas, pointers, welcomed.

Warmly John

Comment on Writing a negative regex using =~
Re: Writing a negative regex using =~
by Anonymous Monk on Oct 17, 2012 at 09:50 UTC
    my( $flag, $regex ) = /(\s*(\!))?(.*)/; $flag ? text !~ $regex : $text =~ $regex
Re: Writing a negative regex using =~
by kcott (Abbot) on Oct 17, 2012 at 09:56 UTC

    G'day John,

    Update: My apologies. I thought I'd read your post fully - I must be going blind. You clearly did explain why you wanted to do this. Ignore the crap I wrote. Sorry!

    Perhaps if you explained why you wanted to do this, you'd get a better answer. However, to do what you describe, why not just negate the result of =~:

    $ perl -Mstrict -Mwarnings -E ' my $x = q{L,0010,8001149805,003,20121015,0600,267,204193,20121015,1300 +,,GBFER,204193}; my $y = q{L,0010,8001149805,003,20121015,0600,267,204193,20121015,1300 +,,XXXXX,204193}; if (! ($x =~ /(?:GBFER|GBFOO)/)) { say "match" } else { say "no match" + } if (! ($y =~ /(?:GBFER|GBFOO)/)) { say "match" } else { say "no match" + } ' no match match

    -- Ken

Re: Writing a negative regex using =~
by betterworld (Deacon) on Oct 17, 2012 at 10:01 UTC

    If I understand your problem correctly, you need a negative look-ahead:

    /^(?!.*(?:GBFER|GBFOO))/

    However I suggest redesigning your system so that it does not prevent you from using !~.

      Heh, given the amount of time I've spent on the regex, that would be the better option!
      That's just the ticket, ta muchly!
Re: Writing a negative regex using =~
by ftumsh (Scribe) on Oct 17, 2012 at 10:54 UTC
    Problem solved. Thank you all for you time.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999506]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2014-09-18 03:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (105 votes), past polls