Matching repetitions with /g

Discussion in 'Perl Misc' started by Wolfram Humann, Aug 18, 2011.

  1. Can I modify

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


    to capture all 4 numbers?
    Wolfram Humann, Aug 18, 2011
    #1
    1. Advertising

  2. Wolfram Humann

    Guest

    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
    , Aug 18, 2011
    #2
    1. Advertising

  3. >>>>> "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
    Charlton Wilbur, Aug 18, 2011
    #3
  4. Wolfram Humann

    Jim Gibson Guest

    In article
    <>,
    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
    Jim Gibson, Aug 18, 2011
    #4
  5. Wolfram Humann

    C.DeRykus Guest

    On Aug 18, 11:08 am, Jim Gibson <> wrote:
    > In article
    > <>,
    >
    > 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


    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
    C.DeRykus, Aug 19, 2011
    #5
  6. Wolfram Humann

    Guest

    On Fri, 19 Aug 2011 13:42:37 -0700 (PDT), "C.DeRykus" <> wrote:

    >On Aug 18, 11:08 am, Jim Gibson <> wrote:
    >> In article
    >> <>,
    >>
    >> 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

    >
    >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
    , Aug 20, 2011
    #6
  7. 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
    Wolfram Humann, Aug 23, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darin dimitrov
    Replies:
    4
    Views:
    1,295
  2. Branka
    Replies:
    4
    Views:
    882
    benben
    Apr 4, 2006
  3. nick048

    How to count some repetitions

    nick048, Nov 1, 2006, in forum: C++
    Replies:
    2
    Views:
    340
    Daniel T.
    Nov 1, 2006
  4. nick048

    How to count some repetitions

    nick048, Nov 1, 2006, in forum: C++
    Replies:
    0
    Views:
    295
    nick048
    Nov 1, 2006
  5. tony
    Replies:
    1
    Views:
    126
    Jürgen Exner
    Jul 23, 2003
Loading...

Share This Page