including slash inside group

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

Moderator: General Moderators

Post Reply
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

including slash inside group

Post by jaymoore_299 »

using this pattern
preg_match_all("|http[://]+[A-Za-z0-9_.-]+|", $contents , $matches);
for string
http://some-domain-name.com

gives the result http://some-domain-name.com

however, if I include a slash, whether by itself or escaped
preg_match_all("|http[://]+[A-Za-z0-9_.-/]+|", $contents , $matches);
or
preg_match_all("|http[://]+[A-Za-z0-9_.-\/]+|", $contents , $matches);

I get the result
http://some

How do I include slashes?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your regex is still going to match lots of incorrect data. Here's a push in the right direction

Code: Select all

preg_match_all('`https?://[A-z0-9_-][A-z0-9_\./-]+`',$text,$matches);
I can recommend you download and use "Regex Coach"
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Thanks for the tip Feyd. Regex Coach is very cool!

http://www.weitz.de/regex-coach/#install
Post Reply