find which subgroups don't match in regex

Discussion in 'Perl Misc' started by Shoryuken, Jul 17, 2008.

  1. Shoryuken

    Shoryuken Guest

    Hello gents, here's the thing been confusing me for a while:

    $regex="(\w+)\s([0-9]+)";

    $a="Tom 1990"; # it's a match
    $b="Jack xyz"; # not a match, because of $2 doesn't match ... but
    here's my question, exactly how to inform the users of this unmatched
    subgroup? (i.e. $2 is the problem, $1 is fine, etc.)

    For a regex matching, is there a way to find which subgroups don't
    match?

    thanks in advance.
     
    Shoryuken, Jul 17, 2008
    #1
    1. Advertising

  2. Shoryuken

    Ben Morrow Guest

    Quoth Shoryuken <>:
    > Hello gents, here's the thing been confusing me for a while:
    >
    > $regex="(\w+)\s([0-9]+)";
    >
    > $a="Tom 1990"; # it's a match
    > $b="Jack xyz"; # not a match, because of $2 doesn't match ... but
    > here's my question, exactly how to inform the users of this unmatched
    > subgroup? (i.e. $2 is the problem, $1 is fine, etc.)
    >
    > For a regex matching, is there a way to find which subgroups don't
    > match?


    You can use /gc and \G to match one piece at a time, without losing your
    place; something like

    my @matches = qw/ \w+ \s [0-9]+ /;
    my $string = 'Jack xyz';

    for my $match (@matches) {
    $string =~ /\G$match/gc
    or print "$match failed at position " . pos $string;
    }

    Ben

    --
    Outside of a dog, a book is a man's best friend.
    Inside of a dog, it's too dark to read.
    Groucho Marx
     
    Ben Morrow, Jul 17, 2008
    #2
    1. Advertising

  3. Shoryuken

    Guest

    Shoryuken <> wrote:
    > Hello gents, here's the thing been confusing me for a while:
    >
    > $regex="(\w+)\s([0-9]+)";
    >
    > $a="Tom 1990"; # it's a match
    > $b="Jack xyz"; # not a match, because of $2 doesn't match ... but
    > here's my question, exactly how to inform the users of this unmatched
    > subgroup? (i.e. $2 is the problem, $1 is fine, etc.)
    >
    > For a regex matching, is there a way to find which subgroups don't
    > match?


    There isn't a built-in way. You'd have to build it yourself, and that
    will probably be non-trivial, as it would pretty much have to be an expert
    system in your exact context, not just some standard Perl feature.

    For example, whose "fault" is it that this doesn't match:

    "1990 Tom" =~ /(\w+)\s(\d+)/;

    Both subgroups will match individually, just not when put together.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Jul 17, 2008
    #3
  4. Shoryuken

    Shoryuken Guest

    On Jul 17, 5:02 pm, Ben Morrow <> wrote:
    > Quoth Shoryuken <>:
    >
    > > Hello gents, here's the thing been confusing me for a while:

    >
    > > $regex="(\w+)\s([0-9]+)";

    >
    > > $a="Tom 1990"; # it's a match
    > > $b="Jack xyz"; # not a match, because of $2 doesn't match ... but
    > > here's my question, exactly how to inform the users of this unmatched
    > > subgroup? (i.e. $2 is the problem, $1 is fine, etc.)

    >
    > > For a regex matching, is there a way to find which subgroups don't
    > > match?

    >
    > You can use /gc and \G to match one piece at a time, without losing your
    > place; something like
    >
    > my @matches = qw/ \w+ \s [0-9]+ /;
    > my $string = 'Jack xyz';
    >
    > for my $match (@matches) {
    > $string =~ /\G$match/gc
    > or print "$match failed at position " . pos $string;
    > }
    >
    > Ben
    >
    > --
    > Outside of a dog, a book is a man's best friend.
    > Inside of a dog, it's too dark to read.
    > Groucho Marx


    This is a great idea, thanks!

    And thanks the other guys for the good input, too!
     
    Shoryuken, Jul 18, 2008
    #4
    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. hiwa
    Replies:
    0
    Views:
    658
  2. wheel
    Replies:
    3
    Views:
    325
    wheel
    Feb 18, 2006
  3. Replies:
    3
    Views:
    835
    Reedick, Andrew
    Jul 1, 2008
  4. Dave
    Replies:
    8
    Views:
    168
    Dave Weaver
    Aug 10, 2005
  5. Sara
    Replies:
    1
    Views:
    103
Loading...

Share This Page