Multiple Captures under one group

Discussion in 'Perl Misc' started by amyl@paxemail.com, Jun 29, 2005.

  1. Guest

    I am having a hard time working out a regular expression that can
    capture multiple entries under a group.

    Here is the text I am working with
    04/15/2005 00:01:37 G3C8F49 Results group [groups=7]: Sloan=SET
    Jones=OK JBUILD1=FAIL

    My goal is to have a "group" called groupnames that captures "Sloan,
    Jones, JBUILD1". I have worked the regular expression I have into
    being able to grab "Sloan", but I can't seem to figure out how to
    capture Jones and JBUILD1 as well.

    Here is the expression that I have up to that point.
    (?<Date>(\d\d/\d\d/\d+ \d\d:\d\d:\d\d)) (?<GROUPID>(G\w+)) Results
    group \[groups=(?<groupsnum>([\-\d]*))\]

    Is what I am asking possible?
    Thanks
    Amy.
     
    , Jun 29, 2005
    #1
    1. Advertising

  2. wrote:
    > I am having a hard time working out a regular expression that can
    > capture multiple entries under a group.
    >
    > Here is the text I am working with
    > 04/15/2005 00:01:37 G3C8F49 Results group [groups=7]: Sloan=SET
    > Jones=OK JBUILD1=FAIL
    >
    > My goal is to have a "group" called groupnames that captures "Sloan,
    > Jones, JBUILD1". I have worked the regular expression I have into
    > being able to grab "Sloan", but I can't seem to figure out how to
    > capture Jones and JBUILD1 as well.
    >
    > Here is the expression that I have up to that point.

    [complex RE snipped]

    You know, it always amazes me to what length people go to come up with the
    most intriguing REs for a problem that could be solved much easier with two
    or three lines of very straight-forward, simple code. Why not just

    use warnings; use strict;
    my$text = '04/15/2005 00:01:37 G3C8F49 Results group [groups=7]: Sloan=SET
    Jones=OK JBUILD1=FAIL';
    my @res=(split(/ /, $text))[-3..-1];
    for (@res) {s/=.*$//;}
    print "@res";

    jue
     
    Jürgen Exner, Jun 29, 2005
    #2
    1. Advertising

  3. wrote:
    > I am having a hard time working out a regular expression that can
    > capture multiple entries under a group.
    >
    > Here is the text I am working with
    > 04/15/2005 00:01:37 G3C8F49 Results group [groups=7]: Sloan=SET
    > Jones=OK JBUILD1=FAIL
    >
    > My goal is to have a "group" called groupnames that captures "Sloan,
    > Jones, JBUILD1".


    my @groupnames;
    push @groupnames, $1 while /(\w+)=\w+(?:\s+|$)/g;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 29, 2005
    #3
  4. <> wrote:

    > I am having a hard time working out a regular expression that can
    > capture multiple entries under a group.



    m//g in list context should do the trick nicely.


    > Here is the text I am working with
    > 04/15/2005 00:01:37 G3C8F49 Results group [groups=7]: Sloan=SET
    > Jones=OK JBUILD1=FAIL



    Is that really the text you are working with, or did your "helpful"
    news posting host word-wrap it for you, thereby breaking it?

    The devil is in the details with pattern matching, we absolutely *must*
    have the correct string to match.

    Such problems can be avoided if you speak Perl rather than English,
    as I've done in the code below.

    Have you seen the Posting Guidelines that are posted here frequently?


    > My goal is to have a "group" called groupnames that captures "Sloan,
    > Jones, JBUILD1". I have worked the regular expression I have into
    > being able to grab "Sloan", but I can't seem to figure out how to
    > capture Jones and JBUILD1 as well.



    -----------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    $_ = '04/15/2005 00:01:37 G3C8F49 Results group [groups=7]: Sloan=SET '
    . 'Jones=OK JBUILD1=FAIL';

    my @groupnames = grep $_ ne 'groups', /(\w+)=/g;

    print "'$_'\n" for @groupnames;
    -----------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 29, 2005
    #4
  5. * Tad McClellan schrieb:
    > <> wrote:
    >
    >> My goal is to have a "group" called groupnames that captures "Sloan,
    >> Jones, JBUILD1". I have worked the regular expression I have into
    >> being able to grab "Sloan", but I can't seem to figure out how to
    >> capture Jones and JBUILD1 as well.

    >
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > $_ = '04/15/2005 00:01:37 G3C8F49 Results group [groups=7]: Sloan=SET '
    > . 'Jones=OK JBUILD1=FAIL';
    >
    > my @groupnames = grep $_ ne 'groups', /(\w+)=/g;


    Seems there is always a space in front of the names. We could simply
    catch all strings between " " and "=".

    my @groupnames = / (\w+)=/g;

    regards,
    fabian
     
    Fabian Pilkowski, Jun 30, 2005
    #5
    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:
    465
    Toby A Inkster
    Jan 19, 2004
  2. El Gato

    Regexp captures

    El Gato, Jan 30, 2007, in forum: Ruby
    Replies:
    2
    Views:
    95
    El Gato
    Jan 31, 2007
  3. Ari Brown

    Regexp: named captures

    Ari Brown, Aug 20, 2007, in forum: Ruby
    Replies:
    20
    Views:
    345
    Steve Austen
    Nov 29, 2010
  4. Sarah Allen
    Replies:
    7
    Views:
    178
    Roger Pack
    Mar 2, 2010
  5. Markus Fischer
    Replies:
    9
    Views:
    179
    7stud --
    Apr 8, 2011
Loading...

Share This Page