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

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

by Kyshtynbai (Acolyte)
on Dec 12, 2012 at 17:13 UTC ( #1008535=perlquestion: print w/ replies, xml ) Need Help??
Kyshtynbai has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone!

I'm learning perl using "Lama Book" and now I'm in the world of regexps. There's an exercise in d foy's book of exercises, which I still can't solve :(. Here it is:

"Write a program to match a string that contains an a, but doesnt have a b anywhere after the a ".

I've written the following regexp, and is _seems_ to be proper (I've run some tests. I really does the job as far as I notice.), but in the answers section of this book there's a rather weired way to solve this exercise, which includes two "if" blocks.

Here is the regexp I've written: /\ba\b|(\b.*a[^b]+\b)/

Is it okay for this exercise, that's all I want to know :) . Thank you in advance.

Comment on Find all strings containing "a" characters, that are not followed by "b" characters.
Download Code
Re: Find all strings containing "a" characters, that are not followed by "b" characters.
by bart (Canon) on Dec 12, 2012 at 17:59 UTC
    Try
    /a(?!b)/
    with negative lookahead.

    And yes, your regexp looks rather OK, except I would drop a few \b anchors:

    /a\b|a[^b]/
    or combined:
    /a(\b|[^b])/
    Will that do?
      immediately after, and anywhere after, not the same :)
Re: Find all strings containing "a" characters, that are not followed by "b" characters.
by Anonymous Monk on Dec 12, 2012 at 17:59 UTC

    No :)

    A string is not a word, a string is a string ... so all your \b is bogus

    simple solution  if( /a/ and not /a.*b/ ){ ... }

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

    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.
      Thank you everyone!

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

        a good boy!
        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!

      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: perlquestion [id://1008535]
Approved by tobyink
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-12-26 01:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls