Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

REGEX Help for Domain Name + Path

Posted on 2014-03-07
5
Medium Priority
?
367 Views
Last Modified: 2014-03-07
Hello,

I am sifting through a page of text and want to use preg_match on it to find urls that match a profile and capture them.

What is the regex I would use to find "http://domain.com/page/name" with:

"domain" could be "domain1" or "mydomain3"
"page/name" being any string with or without slashes, repeated any number of times
the "http" should also look for "https"
there might be a www or no www


Thank you very much!







Thank you!
0
Comment
Question by:EffinGood
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 total points
ID: 39913324
Try
'%http[s]{0,1}://(www\.){0,1}(domain1|mydomain3)\.com(/\w+(\.\w*)*)*%'

Open in new window

HTH,
Dan
0
 

Author Closing Comment

by:EffinGood
ID: 39913382
Thanks Dan. I think I love you.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39913399
Glad I could help!

But I'm afraid I don't feel the same way... :)
0
 

Author Comment

by:EffinGood
ID: 39913652
That's ok. I understand. We can still be friends. :)
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39914227
I've been asked on another forum to
1. stop using {0,1} and use the optional operator (?), the reason being that it's easier to read for the "properly" trained regexp specialists.
2. use non capturing groups (?:) when possible, to speed up matches a little (because the  regex engine does not need to keep track of groups).

So, you have below the functionally equivalent regex, but a little more "canonically" written:

'%https?://(?:www\.)?(?:domain1|mydomain3)\.com(?:/\w*(?:\.\w*)*)*%'

Open in new window

HTH,
Dan
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question