Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Matching repetitions with /g

Reply
Thread Tools

Matching repetitions with /g

 
 
Wolfram Humann
Guest
Posts: n/a
 
      08-18-2011
Can I modify

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


to capture all 4 numbers?

 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      08-18-2011
On Thu, 18 Aug 2011 06:25:57 -0700 (PDT), Wolfram Humann <> wrote:

>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

 
Reply With Quote
 
 
 
 
Charlton Wilbur
Guest
Posts: n/a
 
      08-18-2011
>>>>> "WH" == Wolfram Humann <> writes:

WH> Can I modify

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


Yes. You can start by getting rid of the anchoring ^ and $ and #.

Charlton


--
Charlton Wilbur

 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      08-18-2011
In article
<7d42dfd1-68c0-4c1c-a6d4->,
Wolfram Humann <> wrote:

> 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

--
Jim Gibson
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      08-19-2011
On Aug 18, 11:08*am, Jim Gibson <jimsgib...@gmail.com> wrote:
> In article
> <7d42dfd1-68c0-4c1c-a6d4-75344a11c...@x11g2000yqx.googlegroups.com>,
>
> Wolfram Humann <w.c.hum...@arcor.de> wrote:
> > 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)'

--
Charles DeRykus



 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      08-20-2011
On Fri, 19 Aug 2011 13:42:37 -0700 (PDT), "C.DeRykus" <> wrote:

>On Aug 18, 11:08*am, Jim Gibson <jimsgib...@gmail.com> wrote:
>> In article
>> <7d42dfd1-68c0-4c1c-a6d4-75344a11c...@x11g2000yqx.googlegroups.com>,
>>
>> Wolfram Humann <w.c.hum...@arcor.de> wrote:
>> > 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
 
Reply With Quote
 
Wolfram Humann
Guest
Posts: n/a
 
      08-23-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count some repetitions nick048 C++ 2 11-01-2006 12:43 PM
How to count some repetitions nick048 C++ 0 11-01-2006 10:22 AM
variation with repetitions in C++ Branka C++ 4 04-04-2006 12:49 PM
How to generate all possible permutations with repetitions? darin dimitrov C Programming 4 10-27-2004 08:18 AM
count word repetitions for each pair of lines tony Perl Misc 1 07-23-2003 02:22 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57