Re: perl extracting substrings from string
- From: "kens" <kenslaterpa@xxxxxxxxxxx>
- Date: 28 Dec 2006 15:20:55 -0800
archilleswaterland@xxxxxxxxxxx wrote:
$tina = abc_mn123_ln1xy8_dkxhs
I want to get
$mnval = 123;
$lnval = 1;
$xyval = 8;
without using substr thrice.
I tried this .. but this is wrong syntax.
($mnval,$lnval,$xyval) =~ m/^abc_mn(\d\d\d)_ln(\d)xy(\d)_.*$/;
Is there a way I can do this in one go, using a search or match reg
expression ?
thanks.
Your regular expression would work, but you don't give a string for the
expression
to be matched against:
use strict;
use warnings;
my $tina = 'abc_mn123_ln1xy8_dkxhs';
my ($mnval,$lnval,$xyval) = $tina =~
m/^abc_mn(\d\d\d)_ln(\d)xy(\d)_.*$/;
Of course the regular expression you have given is probably a little to
strict
(e.g. are the values you seek always the same length, and you don't
really care about the start and end of the string I assume). The
statement below is a little more flexible:
my ($mnval,$lnval,$xyval) = $tina =~ m/_mn(\d+)_ln(\d+)xy(\d+)/;
Without actually knowing how your data is formatted, I could
misinterpret something.
HTH, Ken
.
- References:
- perl extracting substrings from string
- From: archilleswaterland@xxxxxxxxxxx
- perl extracting substrings from string
- Prev by Date: Re: better way to skip first few lines of file read?
- Next by Date: Re: better way to skip first few lines of file read?
- Previous by thread: Re: perl extracting substrings from string
- Next by thread: HoA question
- Index(es):
Relevant Pages
|
|