Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REGEX: Capture multiple instances from one line (/g)?

Status
Not open for further replies.

zackiv31

Programmer
May 25, 2006
148
US
I have a variable that contains the html dump of a webpage.. lets call it $html.

I want to extract all the e-mails from it, as well as all the phone numbers.

I don't have a problem with regex's in general.. but if there are more then 1 e-mail or phone number on a line, how do I capture them all? (I know the /g option, but how do i step through each instance..)

I want to end up with two comma seperated lists, one for e-mails, and one for phone numbers.

Example/Pseudo code appreciated.
 
To capture all matches on a line, you can do this:
Code:
my @matches = $string =~ /(regexp for email etc)/g;
 
This doesn't work for me:

Code:
   if (@phones = $html =~ /(\d{3}).*(\d{3}).*(\d{3})/g){
	foreach (@phones){
	    $data{'PHONE'} = ", $1-$2-$3";
	}
    }
 
That works great prex1... I don't understand the .*?, but it works.

Now if only I could exclude html from being caught in this regex... ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top