http://qs1969.pair.com?node_id=826896


in reply to Re^2: regex trouble
in thread regex trouble

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