Gossamer Forum
Home : General : Perl Programming :

poor at Regex...

Quote Reply
poor at Regex...
$str = "John D. Doe, SMD";

Want to extract all (\s)caps before (,) (to look like Initials, want to convert above string to 'JDD')

but really facing some problems understanding $1,$2,$3???

Sorry but I am remained poor in Regexes?

TIA,

Zeshan.
Quote Reply
Re: [zeshan] poor at Regex... In reply to
Hello zeshan try

$str = "John D. Doe, SMD";
$str =~/(\w)\w+\s+(\w)\.\s+(\w)\w+\,.*/;
$initials = "$1$2$3";
print "Initials are $initials \n";

explanation

the J in John captured as (\w) thus $1 with more letters and then white space \w+\s+ OR John = (\w)\w+

the D in D. captured as (\w) thus $2 and a literal period \. and then white space \s+ OR D. = (\w)\.

the D in Doe captured as (\w) thus $3 with more letters and a literal comma \, OR Doe, = (\w)\w+\,

the rest after that is just accounted for with .*

initials just grabs the values held in the holding $1 , $2 and $3 variables.

thanks cornball

Last edited by:

cornball: Oct 17, 2003, 11:23 PM