Re: perl extracting substrings from string




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

.



Relevant Pages

  • Re: regular expression question
    ... "Syntax" means nothing to Regular ... public string Opened() ... and be able to express them in Regular Expression syntax. ... of word characters, and as I said before, '<' is not a word character. ...
    (microsoft.public.dotnet.general)
  • Re: Get regular expression
    ... own tree structure. ... Expression compares a string character-by character, ... regular expression solution, which was about as close as one could get to ... the structure of the hierarchy can be inferred by using ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Get regular expression
    ... regular expression solution, which was about as close as one could get to ... first string. ... explode "ABLATION" and see subnodes of "ENDOMETRIAL ... "Heart 27.33/2" ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regular expression optimization
    ... position in the replacement array of strings, ... > input string and a MatchEvaluator delegate. ... > The first part required combining the separate Regular Expression strings ...
    (microsoft.public.dotnet.general)
  • Small regular expression parser
    ... the goal was to develop a very simple regular expression parser. ... sets are selected using the % character instead of \. ... into the string of the start of the match and the length of the match. ... Last there are a couple macros to help with captures. ...
    (comp.lang.lisp)