Your pattern is wrong, it will match only lines containing an integer and nothing else because of
^ and
$.
Pattern would be better as
"([+-]?[0-9]\\d*|0)"
In any cases, the pattern is over complicated, I would use
"([+-]?\\d+)"
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[
^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[
^]
Expresso Regular Expression Tool[
^]
RegExr: Learn, Build, & Test RegEx[
^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[
^]
[Update]
Try my pattern and your solution with "test 1 23 -45 test"
You may get surprised.