simple regex matching too much! :(

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

Moderator: General Moderators

Post Reply
cs-web
Forum Commoner
Posts: 27
Joined: Fri Mar 11, 2005 11:57 am

simple regex matching too much! :(

Post by cs-web »

Hi I made this simple regex pattern to match URLs so I can convert them to links.

Code: Select all

(http://[0-9a-z/\.?_&-=;+])+
But when I put it to work on this text
Copy and paste this link, its a 40 minute Concert,<br />
<br />
http://www2.xfm.co.uk/staticweb/avplaye ... layhigh<br />
<br />
its pure <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> rock!<br />
It matches
I dont see how its matching the <br on the end?? If anybody can helf it would be brilliant.

Chris
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

Post by programmermatt »

This works for me:

Code: Select all

(http:\/\/[0-9a-z\/\.?_&-=;]+)<
Though I would personally:

Code: Select all

http:\/\/(.*?)<
cs-web
Forum Commoner
Posts: 27
Joined: Fri Mar 11, 2005 11:57 am

Post by cs-web »

you suggestion wont match the URL in
and matches
in the original text and so still matches "<" at the end.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

What about:

Code: Select all

http:\/\/[0-9a-z\/\.\?_&\-=;\+]+
I may have escaped too much, but I think your problem is escaping things...

Or:

Code: Select all

http:\/\/[^\s<]+
Post Reply