Note that there are some explanatory texts on larger screens.

plurals
  1. PONSRegularExpression to validate URL
    primarykey
    data
    text
    <p>I found this regular expression on a website. It is said to be the best URL validation expression out there and I agree. Diego Perini created it.</p> <p>The problem I am facing is when trying to use it with <code>objective-C</code> to detect URLs on strings. I have tried using options like <code>NSRegularExpressionAnchorsMatchLines</code>, <code>NSRegularExpressionIgnoreMetacharacters</code> and others, but still no luck. </p> <p>Is the expression not well formatted for <code>Objective-C</code>? Am I missing something? Any ideas? </p> <p>I have tried John Gruber's regex, also, but it fails with some invalid URLs.</p> <pre><code> Regular Expression Explanation of expression ^ match at the beginning //Protocol identifier (?: (?:https?|ftp http, https or ftp ):\\/\\/ :// )? optional // User:Pass authentication (?: ^\\s+ non white spaces, 1 or more times (?: :^\\s* : non white spaces, 0 or more times, optionally )?@ @ )? optional //Private IP Addresses ?! Means DO NOT MATCH ahead. So do not match any of the following (?: (?!10 10 10.0.0.0 - 10.999.999.999 (?: \\.\\d{1,3} . 1 to 3 digits, three times ){3} ) (?!127 127 127.0.0.0 - 127.999.999.999 (?: \\.\\d{1,3} . 1 to 3 digits, three times ){3} ) (?!169\\.254 169.254 169.254.0.0 - 169.254.999.999 (?: \\.\\d{1,3} . 1 to 3 digits, two times ){2} ) (?!192\\.168 192.168 192.168.0.0 - 192.168.999.999 (?: \\.\\d{1,3} . 1 to 3 digits, two times ){2} ) (?!172\\. 172. 172.16.0.0 - 172.31.999.999 (?: 1[6-9] 1 followed by any number between 6 and 9 | or 2\\d 2 and any digit | or 3[0-1] 3 followed by a 0 or 1 ) (?: \\.\\d{1,3} . 1 to 3 digits, two times ){2} ) //First Octet IPv4 // match these. Any non network or broadcast IPv4 address (?: [1-9]\\d? any number from 1 to 9 followed by an optional digit 1 - 99 | or 1\\d\\d 1 followed by any two digits 100 - 199 | or 2[01]\\d 2 followed by any 0 or 1, followed by a digit 200 - 219 | or 22[0-3] 22 followed by any number between 0 and 3 220 - 223 ) //Second and Third Octet IPv4 (?: \\. . (?: 1?\\d{1,2} optional 1 followed by any 1 or two digits 0 - 199 | or 2[0-4]\\d 2 followed by any number between 0 and 4, and any digit 200 - 249 | or 25[0-5] 25 followed by any numbers between 0 and 5 250 - 255 ) ){2} two times //Fourth Octet IPv4 (?: \\. . (?: [1-9]\\d? any number between 1 and 9 followed by an optional digit 1 - 99 | or 1\\d\\d 1 followed by any two digits 100 - 199 | or 2[0-4]\\d 2 followed by any number between 0 and 4, and any digit 200 - 249 | or 25[0-4] 25 followed by any number between 0 and 4 250 - 254 ) ) //Host name | or (?: (?: [a-z\u00a1-\uffff0-9]+-? any letter, digit or character one or more times with optional - )* zero or more times [a-z\u00a1-\uffff0-9]+ any letter, digit or character one or more times ) //Domain name (?: \\. . (?: [a-z\u00a1-\uffff0-9]+-? any letter, digit or character one or more times with optional - )* zero or more times [a-z\u00a1-\uffff0-9]+ any letter, digit or character one or more times )* zero or more times //TLD identifier (?: \\. . (?: [a-z\u00a1-\uffff]{2,} any letter, digit or character more than two times ) ) ) //Port number (?: :\\d{2,5} : followed by any digit, two to five times, optionally )? //Resource path (?: \\/[^\\s]* / followed by an optional non space character, zero or more times )? optional $ match at the end </code></pre> <p><strong>EDIT</strong> I think I forgot to say that I am using the expression in the following code: (partial code) </p> <pre><code>NSError *error = NULL; NSRegularExpression *detector = [NSRegularExpression regularExpressionWithPattern:[self theRegularExpression] options:0 error:&amp;error]; NSArray *links = [detector matchesInString:theText options:0 range:NSMakeRange(0, theText.length)]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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