Home : General : Perl Programming :

General: Perl Programming: Re: [zeshan] poor at Regex...: Edit Log

Here is the list of edits for this post
Re: [zeshan] poor at Regex...
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

Edit Log: