Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Regex problem

by Anonymous Monk
on Jul 04, 2002 at 14:58 UTC ( #179471=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need a regex to match and split lines of the form
............ keyword=nn ............

where ..'s represent variable numbers of any char.

I would like $1 to be anything upto and including the =

$2 to be nn (can be 1 2 or 3 decimal digits)

$3 to be everything including the space(s) to the end of the line.

I thought this would do it

while(<>) { m/^(.+?\s+keyword=)(\d+)(\s+.+)$/ print "$1-" if $1; print "$2-" if $2; print "$3" if $3; print "\n"; }

But I get nothing?

Comment on Regex problem
Select or Download Code
Re: Regex problem
by Abigail-II (Bishop) on Jul 04, 2002 at 15:12 UTC
    Well, the code you have there doesn't even compile (it's missing a semi colon). So, you didn't cut and paste it. What's your real code you have a problem with? (No, this is not an invitation to cut and paste a gazillion lines of code - write a short program that doesn't do what you expect it to do, test it, and cut and paste that).

    If I add the semi colon, I *do* get the expected output. Giving

    ............ keyword=13 ............
    as input produces
    ............ keyword=-13- ............
    as output.

    Perhaps your input doesn't contain what you expect it to contain.

    Abigail

      Thanks Abigail & Kurt for saving my sanity.

      The missing semi-colon was a typo:(

      The problem, once I had stopped focusing on the regex was a line at the top of the loop that did

      next if m/keyword/;

      I some how deleted the ! from in front of the m//? It was there earlier....grrrr.

        just a sidenote: this additional check will not increase the performance of your script but actually slow it down.

        ---- kurt
Re: Regex problem
by amphiplex (Monk) on Jul 04, 2002 at 15:13 UTC
    your script worked for me, did you maybe test with "keyword=nn" instead of for example "keyword=12" ?
    This will not work of course, as you are matching digits after the "=".

    And you forgot the ";" in the line with the regex, but I think this was only a typo.

    ---- kurt
Re: Regex problem
by DamnDirtyApe (Curate) on Jul 04, 2002 at 15:14 UTC

    I'm not sure if the leading ..'s in your example are supposed to be part of the keyword or not. If not, as your example shows, try this:

    #! /usr/bin/perl use strict ; use warnings ; my @str = ( 'fdjsflsdfjk foo=123 sdjflsdfjklsfj ', 'sdfhsdfsd bar=42 fkjfjklsfjs', 'zoot=23 ff ', ' nooka=9 ' ) ; foreach (@str) { /([[:alnum:]]+)=([[:digit:]]{1,3})(.*)$/ ; printf "[%s] [%s] [%s]\n", $1, $2, $3 ; }
    Output:
    [foo] [123] [ sdjflsdfjklsfj ] [bar] [42] [ fkjfjklsfjs] [zoot] [23] [ ff ] [nooka] [9] [ ]

    Otherwise, if you want to capture the beginning of the string up to the = sign as the keyword regardless of content, maybe this is what you want:

    #! /usr/bin/perl use strict ; use warnings ; my @str = ( 'fdjsflsdfjk foo=123 sdjflsdfjklsfj ', 'sdfhsdfsd bar=42 fkjfjklsfjs', 'zoot=23 ff ', ' nooka=9 ' ) ; foreach (@str) { /^([^=]+)=([[:digit:]]{1,3})(.*)$/ ; printf "[%s] [%s] [%s]\n", $1, $2, $3 ; }
    Output:
    [fdjsflsdfjk foo] [123] [ sdjflsdfjklsfj ] [sdfhsdfsd bar] [42] [ fkjfjklsfjs] [zoot] [23] [ ff ] [ nooka] [9] [ ]

    Hope that helps. :-)


    _______________
    D a m n D i r t y A p e
    Home Node | Email
Re: Regex problem
by vladb (Vicar) on Jul 04, 2002 at 15:15 UTC
    What input are you trying?

    If indeed you've tried this string:
    ............ keyword=nn ............
    Then the regexp you've used won't work. It'll brake right after the 'keyword=' part as 'nn' string doesn't match '\d+' expression.

    However, this works:
    while(<DATA>) { m/^(.+?\s+keyword=)(\d+)(\s+.+)$/; print "$1-" if $1; print "$2-" if $2; print "$3" if $3; print "\n"; } __DATA__ ............ keyword=12 ............


    _____________________
    # Under Construction
Re: Regex problem
by screamingeagle (Curate) on Jul 05, 2002 at 00:14 UTC
    this regex seems to work too :
    m/^(.*\s+keyword=)(\d+)(\s+.+)$/ (the +? has been replaced by *)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://179471]
Approved by davis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-04-20 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls