Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

regex trouble

by blackgoat (Acolyte)
on Mar 05, 2010 at 06:08 UTC ( #826870=perlquestion: print w/replies, xml ) Need Help??

blackgoat has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I have to match the following pattern in a given text in perl:

v09.11-e020

And i've tried the following pattern:

  m/^v[0-9][0-9]\.[0-9][0-9]-[a-z][0-9][0-9][0-9]/

but this doesnt seem to work, what am I doing wrong?? I'm just beginning on regex.

Thanks!

BG

Replies are listed 'Best First'.
Re: regex trouble
by NetWallah (Canon) on Mar 05, 2010 at 06:20 UTC
    Works for me.
    perl -e 'my $x=q|v09.11-e020|;$x =~ m/^v[0-9][0-9]\.[0-9][0-9]-[a-z][ +0-9][0-9][0-9]/ and print " match\n"' #Prints ' match'
    You may want to make the regex somewhat shorter:
    m/^v\d{2}\.\d{2}-[a-z]\d{3}/

         Theory is when you know something, but it doesn't work.
        Practice is when something works, but you don't know why it works.
        Programmers combine Theory and Practice: Nothing works and they don't know why.         -Anonymous

      But if there's a lot of text like a paragraph, and this string needs to be picked out and copied, then how should i do it?? can u pls help??

        You need to capture it (note the parentheses):

        #!/usr/bin/perl -l my $para = "xyz-xyz-xyz-xyz-v09.11-e020xyz-xyz-xyz-"; my ($string) = $para =~ m/(v\d{2}\.\d{2}-[a-z]\d{3})/; print $string; # v09.11-e020

        Matching by itself only checks whether a string conforms to a pattern that you defined in the regular expression.

        You capture strings using brackets - ( ).

        If you have only one string to capture, then the way that almut suggested will work.

        If you have more than one string, you have several ways to capture them:

        First way:

        #!/usr/bin/perl -l my $para = "xyz-xyz-xyz-xyz-v09.11-e020xyz-xyz-xyz-"; my ($part1, $part2) = $para =~ m/(v\d{2}\.\d{2})-([a-z]\d{3})/; print "$part1, $part2"; # v09.11, e020

        Second way:

        #!/usr/bin/perl -l my $para = "xyz-xyz-xyz-xyz-v09.11-e020xyz-xyz-xyz-"; if ($para =~ m/(v\d{2}\.\d{2})-([a-z]\d{3})/) { my ($part1, $part2) = ($1, $2); # the variables $1 and $2 are crea +ted automatically after a successful match print "$part1, $part2"; # v09.11, e020 }

        Third way - very readable, but will only work in Perl 5.10:

        #!/usr/bin/perl -l my $para = "xyz-xyz-xyz-xyz-v09.11-e020xyz-xyz-xyz-"; if ($para =~ m/(?<part1>v\d{2}\.\d{2})-(?<part2>[a-z]\d{3})/) { print "$+{part1}, $+{part2}"; # v09.11, e020 }

        (You should probably pick better names that "part1" and "part2". I only gave them as an example.)

Re: regex trouble
by planetscape (Chancellor) on Mar 05, 2010 at 06:54 UTC
Re: regex trouble
by CountZero (Bishop) on Mar 05, 2010 at 07:16 UTC
    The caret ("^") in your regex means that the pattern is anchored at the beginning of the string, so if your v09.11-e020 string can be found anywhere in the paragraph, this regex will only match if it is at the very beginning of the paragraph. You probably want to drop the caret.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: regex trouble
by Anonymous Monk on Mar 05, 2010 at 09:16 UTC
    use YAPE::Regex::Explain; print YAPE::Regex::Explain->new( '/^v[0-9][0-9]\.[0-9][0-9]-[a-z][0-9] +[0-9][0-9]/' )->explain; __END__ The regular expression: (?-imsx:/^v[0-9][0-9]\.[0-9][0-9]-[a-z][0-9][0-9][0-9]/) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- / '/' ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- v 'v' ---------------------------------------------------------------------- [0-9] any character of: '0' to '9' ---------------------------------------------------------------------- [0-9] any character of: '0' to '9' ---------------------------------------------------------------------- \. '.' ---------------------------------------------------------------------- [0-9] any character of: '0' to '9' ---------------------------------------------------------------------- [0-9] any character of: '0' to '9' ---------------------------------------------------------------------- - '-' ---------------------------------------------------------------------- [a-z] any character of: 'a' to 'z' ---------------------------------------------------------------------- [0-9] any character of: '0' to '9' ---------------------------------------------------------------------- [0-9] any character of: '0' to '9' ---------------------------------------------------------------------- [0-9] any character of: '0' to '9' ---------------------------------------------------------------------- / '/' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2020-01-26 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?