Solved

REGEX Help for Domain Name + Path

Posted on 2014-03-07
5
343 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
  • 3
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 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 34

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 34

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

821 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