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

Basically im trying to come up with a regex which can parse the below strings.

The output should be EC, AB or A.

The strings can have number sometimes like 98, 28.5 and I need to extarct the first 2 characters and if it has a space i should get the first character.

InputString = ("ECX3","ABC3X 98","A B3X 28.5");

$InputString =~ s/(\w |\w+)(\D)(\d)X(\w*|\s*|\d*)$/$1/g;

can some one help.

Replies are listed 'Best First'.
Re: Regex Question for parsing a string
by Athanasius (Archbishop) on May 30, 2013 at 03:15 UTC

    Is this what you’re looking for?

    #! perl use strict; use warnings; my @strings = ('ECX3', 'ABC3X 98', 'A B3X 28.5'); for (@strings) { print "$1\n" if /([a-zA-Z]{1,2})/; }

    Output:

    13:13 >perl 633_SoPW.pl EC AB A 13:13 >

    If your requirements are more complex, please explain exactly what is needed.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,