Matching repetitions with /g

S

sln

Can I modify
perl -E'@a= "# + 17 -18 +19 - 20 #" =~ /^ \# \s* ([+-] \s? \d+ \s+)* \# /xg; say qq(@a)'
- 20

to capture all 4 numbers?

Not the way your doing it. There is no variable amount of
capture buffers. In c# they can do it your way. Normal pcre
can't do this.

You can however, validate the form, then capture the details...

perl -E "\"# + 17 -18 +19 - 20 #\" =~ /^\#\s*((?:[+-]\s?\d+\s+)*)\#/ and @a= $1 =~ /([+-]\s?\d+)\s+/g and say qq(@a)"
or
perl -E '"# + 17 -18 +19 - 20 #" =~ /^\#\s*((?:[+-]\s?\d+\s+)*)\#/ and @a= $1 =~ /([+-]\s?\d+)\s+/g and say qq(@a)'

-sln
 
J

Jim Gibson

Wolfram Humann said:
Can I modify
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
 
C

C.DeRykus

Wolfram Humann said:
Can I modify
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)'
 
S

sln

Wolfram Humann said:
Can I modify
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
 
W

Wolfram Humann

Thanks for the replies. Sorry I couldn't comment for the last couple
of days. Yes, the reason for the anchor is indeed that I want to both
validate (that I don't capture arbitrary numbers from *some* line) and
capture. Perhaps it's indeed better to separate those tasks. I was
aware that the /g was pretty useless here, but I thought it served
well to show what I *wanted* to do.

Thanks again,
Wolfram
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top