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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
($fname,$lname,$mail) = $line=~/(\w+)\W+(\w+).*\s( \S+)/;
ASKER
Thanks!
ASKER