Parsing a string

Hi,
I have multiple strings like:
John, Smith can be reached at: john@microsoft.com

I have many strings like this, what I need to do is put John into $fname, Smith into $lname and john@microsoft.com into $mail.
Note this is a structure for every line: First name, Last name, text, email. Name is always first and separated by commas, email is always last.

Thanks.
MAXcomAsked:
Who is Participating?
 
Kim RyanConnect With a Mentor IT ConsultantCommented:
$current_line =~ /^([a-z]+),\s?([a-z]+).*(\w+@\w+\.[a-z]{3}(\.[a-z]{2})?)$/i;

$fname = $1;
$lname = $2;
$mail = $3;

0
 
MAXcomAuthor Commented:
ok i dont quite get this, lets say my line is stored in the $line, what do I do?
0
 
MAXcomAuthor Commented:
ok i dont quite get this, lets say my line is stored in the $line, what do I do?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Kim RyanIT ConsultantCommented:
If you say $line =~ /^{[a-z] ... etc, you are binding the contents of $line to the regular expression. The brackets say to retain matching portions or groups. These can then be retrieved from the special verialbes, $1 for the first group etc and assigned to a local variable in your program.
0
 
ozoCommented:
($fname,$lname,$mail) = $line=~/(\w+)\W+(\w+).*\s(\S+)/;
0
 
MAXcomAuthor Commented:
Thanks!
0
All Courses

From novice to tech pro — start learning today.