Wolfram Humann said:
perl -E'@a= "# + 17 -18 +19 - 20 #" =~ /^ \# \s* ([+-] \s? \d+ \s+)* \#
/xg; say qq(@a)'
- 20
to capture all 4 numbers?
Yes. You need to remove the elements that anchor your pattern to the
beginning and end of the string, and eliminate the repeat modifier '*'
for the sub-pattern that matches the numbers:
perl -E'@a= ("# + 17 -18 +19 - 20 #" =~ /(\s* [+-] \s? \d+)/xg); say
qq(@a)'
+ 17 -18 +19 - 20
Or, it's slower but you could even
workaround the anchoring:
perl -E '@a= "# + 17 -18 +19 - 20 #" =~ /(?:^|) \#? \s* ([+-] \s?
\d+ \s+) \#? /xg; say qq(@a)'
Its meaningless that Wolfram used the /g modifier while anchoring the
regex with a must ^ unless it were used in conjunction with \G assertion.
So the anchors ^ and # would seem to be for validation of what must come
between. It would then be a wasted effort to exclude them, and at the
same time, a wasted effort to include them using /g in this context.
For capturing purposes,
/(?:^|) \#? \s* ([+-] \s? \d+ \s+) \#? /xg
is identical to
/([+-] \s? \d+ \s+)/xg
Both of which when applied to this string
"adfsg+ 17 -18 dsfgh 92+19 - 20 "
return the list
('+ 17 ', '-18 ', '+19 ', '- 20')
-sln