Regular Expression to detect URLS

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

Moderator: General Moderators

Post Reply
phpcoder24july
Forum Newbie
Posts: 3
Joined: Sun Jul 24, 2011 12:24 pm

Regular Expression to detect URLS

Post by phpcoder24july »

Hi everyone,

I am looking for a regular expression which can extract URLS with extension(jpg,png,gif,mp3,mp4,3gp).
At present I have this one $reg_exUrl = "!(http|https)://([a-z0-9A-Z\-\.\/\_\:\@]+\.(?:jpe?g|png|gif|mp3|mp4|3gp))!Ui"
It works well but for this url=http://190.230.80.163:8080/movies/67702 ... 80.163.3gp
It only detects http://190.230.80.163:8080/movies/677025542@190.230

I tried one more $reg_exUrl = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z0-9]{2,3}(\/\S*)?/"
this gives http://190.230

Any help would be appreciable.
Thanks
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Regular Expression to detect URLS

Post by ridgerunner »

Try this one:

[text]$re = '%\bhttps?://\S+\.(?:jpg|png|gif|mp3|mp4|3gp)\b%i';[/text]
phpcoder24july
Forum Newbie
Posts: 3
Joined: Sun Jul 24, 2011 12:24 pm

Re: Regular Expression to detect URLS

Post by phpcoder24july »

Thanks for your reply.
I am getting the same result http://190.230.80.163:8080/movies/677025542@190.230
for http://190.230.80.163:8080/movies/67702 ... 80.163.3gp. For others
its working fine. Anything else to try. I urgently need this one.
Thanks.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Regular Expression to detect URLS

Post by pickle »

What are you running this on? A long string? Or are you just trying to identify which URLs in a list end in the extensions you want?

In any case, I've found with regular expressions that you should only specify what you absolutely need to. For example, is it necessary to specify "://([a-z0-9A-Z\-\.\/\_\:\@]" ?

This regex works in with the short string I've been testing:
[syntax]!(http.*[3gp|jpg|gif|png|mp3|mp4])![/syntax]

Testing string:
oaeuaoeuhttp://190.230.80.163:8080/moviesjpg/677025542@190.230.80.163.jpgaoeuaoeu
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
phpcoder24july
Forum Newbie
Posts: 3
Joined: Sun Jul 24, 2011 12:24 pm

Re: Regular Expression to detect URLS

Post by phpcoder24july »

Actually I need to parse the body of an email to extract all
the urls with extension (mp3,3gp,mov,avi,wmv,jpg,png etc).

You can have a look at this
$text = "hello.. how are you?? are you keeping well?? please checkout this url
http://190.230.80.160:8080/movies/67702 ... 80.160.3gp
http://dailywallpaper.files.wordpress.c ... 00.jpg..//
!??? this is the logo http://s.ytimg.com/yt/img/email_logo_no_tagline.png?? ?
http://190.230.80.160/ISC/Ressources/AF_KLM/afklm.mp4 ./......plj/http://v6.cache2.c.bigcache.googleapis. ... 775554.jpg ";

The regular expressions I have used are working fine for all of the above urls except the first one
in the $text. First one I get only this http://190.230.80.160:8080/movies/677025542@190.230

Thanks
Post Reply