in reply to Help with regular expression

Actually, your regular expression seems correct (although it could possibly be somewhat simpler) for the description you made (except that I am not sure what you really want to capture) and it works, as shown in this session under the Perl debugger.
DB<4> $_ = 'foobar 42'; DB<5> print $1 if /\b(\s*([a-zA-Z ]+)\s((-?[0-9]){1,2})\b)/g; foobar 42 DB<6> $_ = 'foobar 666'; DB<7> print $1 if /\b(\s*([a-zA-Z ]+)\s((-?[0-9]){1,2})\b)/g; DB<8>
As you can see, your regex matched foobar 42 but did not match foobar 422, which appears to be what you want.

If that is not what you want, then you should explain what exactly you want, and preferably, as already suggested, please also show some example of what should match (and what should be captured) and what should not match.

Replies are listed 'Best First'.
Re^2: Help with regular expression
by heman (Novice) on Nov 13, 2014 at 01:20 UTC
    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 :(
      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!
      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.