Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: regex trouble

by NetWallah (Canon)
on Mar 05, 2010 at 06:20 UTC ( [id://826874]=note: print w/replies, xml ) Need Help??


in reply to regex trouble

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

Replies are listed 'Best First'.
Re^2: regex trouble
by blackgoat (Acolyte) on Mar 05, 2010 at 06:31 UTC
    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
        okay... thanks!!

      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.)

        I have to run a command in unix. And I have to match this expression from whatever output I get. I tried doing the following:

        my $var = 'command'; my ($exp) = $var =~ m/(v\d{2}\.\d{2})-([a-z]\d{3})/;

        The same is followed by a html script in which the value of $exp must appear. But I am unable to do so...

        Can u pls help!

        Thanks

        BG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://826874]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found