http://www.perlmonks.org?node_id=1008542


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.

Replies are listed 'Best First'.
Re^2: Find all strings containing "a" characters, that are not followed by "b" characters.
by johngg (Canon) 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

Re^2: Find all strings containing "a" characters, that are not followed by "b" characters.
by Not_a_Number (Prior) 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 Kyshtynbai (Sexton) 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!