Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re: stupid question about regexp - not matching a word

by december (Pilgrim)
on Jul 15, 2002 at 22:21 UTC ( [id://181948]=note: print w/replies, xml ) Need Help??


in reply to Re: stupid question about regexp - not matching a word
in thread stupid question about regexp - not matching a word

Actually, I was trying to match every sentence where 'abc' is not preceding 'nevermind' - every other word is ok.

Negative look-behind works, but I expected a more common combination to work too - something like grouping letters with () to a word, and then ^ to negate the sense. Is there no way to group these letters as a word instead of individual characters?

It's more a matter of curiosity if it can be done with just a simple regexp rather than making it work, otherwise I'd easily solve it with more code.

Thanks! ;)

  • Comment on Re: Re: stupid question about regexp - not matching a word

Replies are listed 'Best First'.
Re: Re: Re: stupid question about regexp - not matching a word
by thelenm (Vicar) on Jul 15, 2002 at 23:04 UTC
    Do you not want to use lookbehind because of efficiency concerns, or some other reason? It seems to be just the right tool for the job... It's doing what you were trying to do in your original code: making sure "abc" does not match before "nevermind".

    As far as using [^abc] to do the job, [] represents a character class within regular expressions, and can only match a single character at a time. So unfortunately, you can't use [] to match anything more than a single character.

    -- Mike

    --
    just,my${.02}

      It's not really because of efficiency concerns, I would like my regexp (if possible) to work with the sadly enough more simple c regexp function in sed and grep. I'm not sure that look-behinds are supported.

      I'm just learning more about regexp's, which is why I wanted to solve it without multiple lines of perl code. I'm a bit surprised that you can group several words in an or-chain, but not negate the matching of a single word without look-behind.

      Not really a problem though, I'll just use look-behinds. Thanks again for your time...

         wouter

Re: stupid question about regexp - not matching a word
by Abigail-II (Bishop) on Jul 16, 2002 at 11:55 UTC
    The Cookbook has the following solution for your problem:
    /^(?:(?!abc).)*$/
    This matches a string that does not contain the text abc.

    Abigail

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 05:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found