웹 사이트에서이 정규 표현식을 발견했습니다. 거기서 최고의 URL 검증 표현이라고 말하면서 나는 동의한다. Diego Perini가 그것을 만들었습니다.URL 유효성을 검사하는 NSRegularExpression
내가 직면 한 문제는 objective-C
과 함께 사용하여 문자열의 URL을 감지하려고 할 때의 문제입니다. 나는 NSRegularExpressionAnchorsMatchLines
, NSRegularExpressionIgnoreMetacharacters
및 다른 사람과 같은 선택권을 시도했다, 그러나 아직도 운이 없다.
표현식이 Objective-C
에 적합하지 않습니까? 내가 놓친 게 있니? 어떤 아이디어?
존 그루버 (John Gruber)의 정규식을 사용해 보았지만 잘못된 URL이 있으면 실패합니다.
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
)[email protected] @
)? 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
편집 나는 내가 다음 코드 표현을 사용하고 말을 잊었다 생각 : (부분 코드)
NSError *error = NULL;
NSRegularExpression *detector = [NSRegularExpression regularExpressionWithPattern:[self theRegularExpression] options:0 error:&error];
NSArray *links = [detector matchesInString:theText options:0 range:NSMakeRange(0, theText.length)];
좋은 게시물에 대한 고마워. – Jhaliya
업데이트 된 Gruber 코드는 좋지만 "google.com"과 일치하지 않지만 "google.comm"및 "google.co.uk"와 일치합니다. – mootymoots
많은 정규식에 감사드립니다. 정말 훌륭합니다. –