Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The first regular expression there is made to match almost everything that follows ftp://, http://, https:// that occurs, so it might be best to implement the others as separate expressions since they'll only be matching hostnames. </p> <p>For number 1, you'll need to decide how strictly you wish to match different TLDs (.com/.net/etc). For example, you can explicitly match them like this: </p> <pre><code>(www\.)?[a-z0-9\-]+\.(com|net|org) </code></pre> <p>However, that will only match URLs that end in .com, .net, or .org. If you want all top-level domains and only the valid ones, you'll need to manually write them all in to the end of that. Alternatively, you can do something like this,</p> <pre><code>(www\.)?[a-z0-9\-]+\.[a-z]{2,6} </code></pre> <p>which will accept anything that looks like a url and ends with "dot", and any combination of 2 to 6 letters (.museum and .travel). However, this will match strings like "fgs.fds". Depending on your application, you may need to add more characters to <code>[a-z]</code>, to add support for extended character alphabets.</p> <p>Edit (2 Aug 14): As pointed out in the comments below, this won't match TLDs like .co.uk. Here's one that will:</p> <pre><code>(www\.)?[a-z0-9\-]+\.([a-z]{2,3}(\.?[a-z]{2,3})?) </code></pre> <p>Instead of any string between two and six characters (following a period), this will match any two to three, then another one to three (if present), with or without a dividing period.</p> <p>It'd be redundant, but you could instead remove the question mark after www on the second option, then do both tests; that way, you can match any string ending in a common TLD, or a string that begins with "www." and is followed by any characters with one period separating them, "gpspps.cobg". It would still match sites that might not actually exist, but at least it looks like a url, at it would look like one.</p> <p>For the YouTube one, I went a little question mark crazy.</p> <pre><code>(?i:(?:(?:http(?:s)?://)?(?:www\.)?)?youtu(?:\.be/|be\.com/watch\?(?:[a-z0-9_\-\%\&amp;\=]){0,}?v\=))([a-zA-Z0-9_\-]{11}){0,}?v\=))(?i)([a-zA-Z0-9_\-]{11}) </code></pre> <p>EDIT: I just tried to use the above regex in one of my own projects, but I encountered some errors with it. I changed it a little and I think this version may be better:</p> <pre><code>(?i:(?:(?:http(?:s)?://)?(?:www\.)?)?youtu(?:\.be/|be\.com/watch\?(?:[a-z0-9_\-\%\&amp;\=]){0,})?)(?:v=)?([a-zA-Z0-9_\-]{11}) </code></pre> <p>For those not familiar with regular expressions, parentheses , <code>( ...regex... )</code>, are stored as groups, which can be selectively picked out of matched strings. Parenthesis groups that begin with <code>?:</code> as in most of the ones up there, <code>(?:www\.)</code> are however not captured within the groups. Because the end of that regex was left as a normal—"captured"—group, <code>([a-zA-Z0-9_\-]{11})</code>, you use the <code>$matches</code> argument of functions like <a href="http://www.php.net/manual/en/function.preg-match.php" rel="nofollow">preg_match</a>, then you can use <code>$matches[1]</code> to get the YouTube ID of the video, 'examplevide', then work with it however you'd like. Also note, the regex is only matching 11 characters for the ID.</p> <p>This regex will match pretty much any of the current youtube url formats including incorrect cases, and out of (normal) order parameters:</p> <pre><code>http://youtu.be/dQw4w9WgXcQ https://www.youtube.com/watch?v=dQw4w9WgXcQ http://www.youtube.com/watch?v=dQw4w9WgXcQ&amp;feature=featured http://www.youtube.com/watch?feature=featured&amp;v=dQw4w9WgXcQ http://WWW.YouTube.Com/watch?v=dQw4w9WgXcQ http://YouTube.Com/watch?v=dQw4w9WgXcQ www.youtube.com/watch?v=dQw4w9WgXcQ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload