multiple return regex question

Discussion in 'Perl Misc' started by vorticitywolfe@gmail.com, Dec 11, 2007.

  1. Guest

    Hello,

    I have a string like such "STAT 09834Z 12004KT VV001 BKN150 OVC220
    A3025 T00281039"

    I want to grab the VV001 BKN150 and OVC220 part in different variables
    $1, $2, $3, etc..

    I am parsing the string with this regex:

    @return = m/((VV|BKN|OVC)\d{2,4})/g;

    and print with

    for(@return){print $_,"\n";}

    Why does it return:
    VV001
    VV
    BKN150
    BKN
    OVC250
    OVC

    ?
    in the regex I think it's somewhat explicit in keeping the first 2 or
    3 characters together and then keeping the rest appended, but perhaps
    I'm misinterpreting what it's doing. Any help would be appreciated.

    Thank you!
    Jonathan
     
    , Dec 11, 2007
    #1
    1. Advertising

  2. writes:

    > I have a string like such "STAT 09834Z 12004KT VV001 BKN150 OVC220
    > A3025 T00281039"
    >
    > I want to grab the VV001 BKN150 and OVC220 part in different variables
    > $1, $2, $3, etc..
    >
    > I am parsing the string with this regex:
    >
    > @return = m/((VV|BKN|OVC)\d{2,4})/g;


    You have two capturing parenteses, this means that each time you
    pattern matches you get two "results". Use non-capturing parenteses
    for the inner group to get what you want:

    m/((?:VV|BKN|OVC)\d{2,4})/g;

    Read the 'Extended Patterns' part of 'perldoc perlre' for more
    information.

    //Makholm
     
    Peter Makholm, Dec 11, 2007
    #2
    1. Advertising

  3. Guest

    On Dec 11, 9:10 pm, Peter Makholm <> wrote:
    > writes:
    > > I have a string like such "STAT 09834Z 12004KT VV001 BKN150 OVC220
    > > A3025 T00281039"

    >
    > > I want to grab the VV001 BKN150 and OVC220 part in different variables
    > > $1, $2, $3, etc..

    >
    > > I am parsing the string with this regex:

    >
    > > @return = m/((VV|BKN|OVC)\d{2,4})/g;

    >
    > You have two capturing parenteses, this means that each time you
    > pattern matches you get two "results". Use non-capturing parenteses
    > for the inner group to get what you want:
    >
    > m/((?:VV|BKN|OVC)\d{2,4})/g;
    >
    > Read the 'Extended Patterns' part of 'perldoc perlre' for more
    > information.
    >
    > //Makholm


    Thanks, worked like a charm!! I knew it had to be something simple! I
    haven't quite got all the metacharacters down, but the Extended
    Patterns should be of great help! Thanks Again!!
     
    , Dec 11, 2007
    #3
    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. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    742
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,685
    Ant...
    Nov 6, 2003
  3. Greenhorn
    Replies:
    15
    Views:
    882
    Keith Thompson
    Mar 6, 2005
  4. Replies:
    3
    Views:
    823
    Reedick, Andrew
    Jul 1, 2008
  5. Damjan Rems

    Regex. How to return multiple lines

    Damjan Rems, Aug 8, 2008, in forum: Ruby
    Replies:
    9
    Views:
    155
    Damjan Rems
    Aug 11, 2008
Loading...

Share This Page