regex to match a URL with optional 'www' a

2020-04-21 09:25发布

问题:

I'm trying to write a regexp.

some background info: I am try to see if the REQUEST_URI of my website's URL contains another URL. like these:

  • http://mywebsite.com/google.com/search=xyz

However, the url wont always contain the 'http' or the 'www'. so the pattern should also match strings like:

  • http://mywebsite.com/yahoo.org/search=xyz
  • http://mywebsite.com/www.yahoo.org/search=xyz
  • http://mywebsite.com/msn.co.uk'
  • http://mywebsite.com/http://msn.co.uk'

there are a bunch of regexps out there to match urls but none I have found do an optional match on the http and www.

i'm wondering if the pattern to match could be something like:

^([a-z]).(com|ca|org|etc)(.)

I thought maybe another option was to perhaps just match any string that had a dot (.) in it. (as the other REQUEST_URI's in my application typically won't contain dots)

Does this make sense to anyone? I'd really appreciate some help with this its been blocking my project for weeks.

Thanks you very much -Tim

回答1:

I suggest using a simple approach, essentially building on what you said, just anything with a dot in it, but working with the forward slashes too. To capture everything and not miss unusual URLs. So something like:

^((?:https?:\/\/)?[^./]+(?:\.[^./]+)+(?:\/.*)?)$

It reads as:

  • optional http:// or https://
  • non-dot-or-forward-slash characters
  • one or more sets of a dot followed by non-dot-or-forward-slash characters
  • optional forward slash and anything after it

Capturing the whole thing to the first grouping.

It would match, for example:

  • nic.uk
  • nic.uk/
  • http://nic.uk
  • http://nic.uk/
  • https://example.com/test/?a=bcd

Verifying they are valid URLs is another story! It would also match:

  • index.php

It would not match:

  • directory/index.php

The minimal match is basically something.something, with no forward slash in it, unless it comes at least one character past the dot. So just be sure not to use that format for anything else.



回答2:

To match an optional part, you use a question mark ?, see Optional Items.

For example to match an optional www., capture the domain and the search term, the regular expression could be

(www\.)?(.+?)/search=(.+)

Although, the question mark in .+? is a non-greedy quantifier, see http://www.regular-expressions.info/repeat.html.



回答3:

You might try starting your regex with

^(http://)?(www\.)?

And then the rules to match the rest of a URL.



回答4:

Here is my two cents :

$regex = "/http:\/\/mywebsite\.com\/((http:\/\/|www\.)?[a-z]*(\.org|\.co\.uk|\.com).*)/";

See the working exemple

But I'm sure you can do better !

Hope it helps.



回答5:

$re = '/http:\/\/mywebsite\.com\/((?:http:\/\/)?[0-9A-Za-z]+(?:-+[0-9A-Za-z]+)*(?:\.[0-9A-Za-z]+(?:-+[0-9A-Za-z]+)*)+(?:\/.*)?)/';

https://regex101.com/r/x6vUvp/1

Obeys the DNS rule that hyphens must be surrounded. Replace http with https? to allow https URLs as well.

According to the list of TLDs at Wikipedia there are at least 1519 of them and it's not constant so you may want to give the domain its own capture group so it can be verified with an online API or a file listing them all.