Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Find all strings containing "a" characters, that are not followed by "b" characters.

by aitap (Deacon)
on Dec 12, 2012 at 18:03 UTC ( #1008542=note: print w/ replies, xml ) Need Help??


in reply to Find all strings containing "a" characters, that are not followed by "b" characters.

It looks like you need a look-ahead assersion:

$string = /a(?!b)/;

EDIT: Well, I misread the question (paid too much attention to the title). A simple /a/ and !/a.*b/ (++1008541) or /^[^a]*a+[^b]*$/ should be enough.

Sorry if my advice was wrong.


Comment on Re: Find all strings containing "a" characters, that are not followed by "b" characters.
Select or Download Code
Re^2: Find all strings containing "a" characters, that are not followed by "b" characters.
by Kyshtynbai (Acolyte) on Dec 12, 2012 at 18:12 UTC
    Thank you everyone!

      Try this simple test string before you plump for any of the solutions given:

      a good boy!
Re^2: Find all strings containing "a" characters, that are not followed by "b" characters.
by Not_a_Number (Parson) on Dec 12, 2012 at 18:18 UTC
      If only I'd knew that logic operators like "and" would work with regular expressions! In my Lama book (5th edition) it is written, that they actualy DO NOT work with regexps (or something has messed up in my head :). I'll check it tomorrow, now gonna sleep). Thank you everyone, guys!

        Well, // is m//, the match operator, it is not a regexp :)

        "logical and" works with m//atch operator, not regexp

Re^2: Find all strings containing "a" characters, that are not followed by "b" characters.
by johngg (Abbot) on Dec 12, 2012 at 23:39 UTC

    A look-ahead is fine, you just have to insert .* (zero or more of anything) before the "b" I think.

    $ perl -E ' > say qq{$_ - @{ [ m{a(?!.*b)} ? q{Match} : q{No match} ] }} for ( > q{Have you any fish}, > q{What a nice broom}, > q{Fishing for trout}, > );' Have you any fish - Match What a nice broom - No match Fishing for trout - No match $

    I hope this is of interest.

    Cheers,

    JohnGG

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-10-22 12:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (117 votes), past polls