in reply to Re: Help with regular expression
in thread Help with regular expression

Guys, Here is what i want my regular expression to do: $_ = 'xyz 12 45' then my regular expression should fail. Also, $_='xyz 123' then my regular expression should fail. But, $_= 'xyz 12' then it should pass. However, i see that the 2nd and 3rd scenario's are workign but, not the 1st one. My regular expression seems to take the 1st one i.e xyz 12 45 as xyz 12 and process the expression. Which is not what i want :(

Replies are listed 'Best First'.
Re^3: Help with regular expression
by toolic (Bishop) on Nov 13, 2014 at 01:45 UTC
    use warnings; use strict; while (<DATA>) { if (/^[a-z]+\s+\d{2}$/i) { print "PASS $_"; } else { print "FAIL $_"; } } __DATA__ xyz 12 45 xyz 123 xyz 12

    outputs:

    FAIL xyz 12 45 FAIL xyz 123 PASS xyz 12
      Thanks toolic :) That works. I guess using boundary made the difference!
Re^3: Help with regular expression
by graff (Chancellor) on Nov 13, 2014 at 02:26 UTC
    toolic's solution is the right answer for the few examples you gave, but since you haven't said anything about the larger context of strings you might be getting as input, how should the following be treated by your intended regex?
    (foo 12) foo 12 bar foo 12, bar 34 Foour score and 12 years ago there were 34 bars in this town.
    All the above would fail, given toolic's very tight use of anchors (^ and $). If that's what you want, then the problem is solved.