regex @a = m / | /g and captures?

Discussion in 'Perl Misc' started by Bill, Oct 17, 2003.

  1. Bill

    Bill Guest

    Hello, I've got a regex question.

    In the following, the use of () in an 'or' type regex causes @a to
    hold both captures, so for each pass through the regex, one capture
    and one undef is stored.

    Can this be prevented and still use () captures and '|' in the regex?


    >>>>>>>>>>>


    my $s = '1 2 {3, 3, 3} 4';

    my @a = $s =~ m/\{[^\}]+\}|\d/g;

    print "\nWithout captures:\n", join "\n", @a;

    @a = $s =~ m/(\{[^\}]+\})|(\d)/g;
    foreach(@a) { $_ = 'undef' unless $_; }
    print "\n\nNow with captures:\n", join "\n", @a;

    <<<<<<<<<<<
    Bill, Oct 17, 2003
    #1
    1. Advertising

  2. Bill <> wrote:
    > In the following, the use of () in an 'or' type regex causes @a to
    > hold both captures, so for each pass through the regex, one capture
    > and one undef is stored.
    >
    > Can this be prevented and still use () captures and '|' in the regex?


    Put the parentheses around the entire expression.

    > @a = $s =~ m/(\{[^\}]+\})|(\d)/g;


    /( { [^}]+ } | \d )/xg;

    But (as you already know) you don't need the parens at all in
    this case.

    --
    Steve
    Steve Grazzini, Oct 17, 2003
    #2
    1. Advertising

  3. Bill <> wrote:

    > In the following, the use of () in an 'or' type regex causes @a to
    > hold both captures, so for each pass through the regex, one capture
    > and one undef is stored.
    >
    > Can this be prevented and still use () captures and '|' in the regex?



    > @a = $s =~ m/(\{[^\}]+\})|(\d)/g;


    grep() is handy when you need to filter a list:

    my @a = grep defined, $s =~ m/(\{[^\}]+\})|(\d)/g;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 17, 2003
    #3
  4. Bill

    Bill Guest

    Steve Grazzini <> wrote in message news:<iXUjb.33$>...
    >
    > Put the parentheses around the entire expression.
    >
    > > @a = $s =~ m/(\{[^\}]+\})|(\d)/g;

    >
    > /( { [^}]+ } | \d )/xg;
    >


    Oh yes, of course! Cool.
    But I think that I simplified the code I was revising too far.

    What about this (we want the numbers not the separators):
    >>>>>>>>>>>


    my $s = '1; 2; {3, 3, 3}; 4;';

    my @a = $s =~ m/\{[^\}]+\};|\d;/g;

    print "\nWithout captures:\n", join "\n", @a;

    @a = $s =~ m/(\{[^\}]+\});|(\d);/g;
    foreach(@a) { $_ = 'undef' unless $_; }
    print "\n\nNow with captures:\n", join "\n", @a;

    <<<<<<<<<<<

    It seems that either I have to chop the answers here or filter undefs,
    as Tad suggests?
    Bill, Oct 18, 2003
    #4
  5. (Bill) wrote in message news:<>...
    > > /( { [^}]+ } | \d )/xg;
    > >

    >
    > Oh yes, of course! Cool.
    > But I think that I simplified the code I was revising too far.
    >
    > What about this (we want the numbers not the separators):
    > >>>>>>>>>>>

    >
    > my $s = '1; 2; {3, 3, 3}; 4;';
    >
    > my @a = $s =~ m/\{[^\}]+\};|\d;/g;


    Then move the common elements (semi-colon) out of the alternation. In
    this case, they can be moved out of the capture as well:

    /( { [^}]+ } | \d );/xg;

    But you haven't stated whether the semi-colons are always there, or
    meaningful. If they have no meaning, you can go with the previous
    version:

    > > /( { [^}]+ } | \d )/xg;


    -QM
    Quantum Mechanic, Oct 18, 2003
    #5
  6. Bill

    Bill Guest

    (Quantum Mechanic) wrote in message news:<>...

    > > >>>>>>>>>>>

    > >
    > > my $s = '1; 2; {3, 3, 3}; 4;';
    > >
    > > my @a = $s =~ m/\{[^\}]+\};|\d;/g;

    >
    > Then move the common elements (semi-colon) out of the alternation. In
    > this case, they can be moved out of the capture as well:
    >
    > /( { [^}]+ } | \d );/xg;
    >
    > But you haven't stated whether the semi-colons are always there, or
    > meaningful. If they have no meaning, you can go with the previous
    > version:
    >
    > > > /( { [^}]+ } | \d )/xg;

    >
    > -QM


    So, I guess the answer in general is just to find a way to rewrite the
    regex so that there is only one capture. It's good that regexes are so
    flexible. Thanks
    Bill, Oct 19, 2003
    #6
    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. Joshua Beall

    Screen captures

    Joshua Beall, Jan 19, 2004, in forum: HTML
    Replies:
    4
    Views:
    449
    Toby A Inkster
    Jan 19, 2004
  2. El Gato

    Regexp captures

    El Gato, Jan 30, 2007, in forum: Ruby
    Replies:
    2
    Views:
    88
    El Gato
    Jan 31, 2007
  3. Todd W

    rename captures in regex

    Todd W, Feb 10, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    158
    Todd W
    Feb 11, 2005
  4. Joe Gottman

    Regex with a varrying number of captures

    Joe Gottman, Jun 18, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    117
    Brian McCauley
    Jun 19, 2005
  5. gorjusborg
    Replies:
    2
    Views:
    91
    gorjusborg
    Sep 22, 2006
Loading...

Share This Page