Link to home
Start Free TrialLog in
Avatar of MAXcom
MAXcom

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MAXcom
MAXcom

ASKER

ok i dont quite get this, lets say my line is stored in the $line, what do I do?
Avatar of MAXcom

ASKER

ok i dont quite get this, lets say my line is stored in the $line, what do I do?
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.
Avatar of ozo
($fname,$lname,$mail) = $line=~/(\w+)\W+(\w+).*\s(\S+)/;
Avatar of MAXcom

ASKER

Thanks!