Matching a URL with Regex?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
FuzzyLogik
Forum Newbie
Posts: 18
Joined: Tue Dec 12, 2006 12:15 am

Matching a URL with Regex?

Post by FuzzyLogik »

I need to match the following:

<a href="(url)" title="do not need">(description)</a>

I want to pull the URL and description from the URL, and I need it to work whether there is a title tag or not.

Anyone feel like helping me out?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would use two patterns. One to pull out the entire link, the second to extract the URL.

Take a look here: viewtopic.php?t=29312
Ciprian
Forum Newbie
Posts: 4
Joined: Fri Feb 02, 2007 10:15 pm

Post by Ciprian »

Hi

Code: Select all

'#<a.*href="([^"]+)"[^>]*(.*)</a>#i'
this works for:

<a href="url1" title="do not need">description1</a>
<a href="url2">description2</a>
<a title="do not need" href="url3">description3</a>

Cheers
Post Reply