sam sam - 2 months ago 8
Perl Question

matching negative number in a line not working

How to match negative number in a line.


input line :strongest_signal_indication Invalid {-1} some text


I was trying this regex expression:

/ ( [+-]?\d+ )| (\s+ ( \{ [^-]+ \} |\S+)) /xg


but it did not work for me

Any help please

Answer

Your pattern actually does match.

$ perl -E'
   say
      "Invalid {-1}" =~ / ( [+-]?\d+ )| (\s+ ( \{ [^-]+ \} |\S+)) /x
         ? "match" : "no match";'
match

In fact, / [+-]? \d+ /x would have sufficed.