import Foundation
let pattern = ##"(?i)(?<scheme>http|https|ftp|sftp|sip|sips|file):\/\/(?:(?<username>[^`!@#$^&*()+=,:;'"{}\|\[\]\s\/\\]+)(?::(?<password>[^`!@#$^&*()+=,:;'"{}\|\[\]\s\/\\]+))?@)?(?:(?<ipv4>((?:(?:25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|1?\d\d?)))|\[(?<ipv6>(?i)(?:[\da-f]{0,4}:){1,7}(?:(?<ipv4_in_ipv6>(?:(?:25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|1?\d\d?))|[\da-f]{0,4}))\]|(?:(?<sub_domain>[^\s~`!@#$%^&*()_+=,.?:;'"{}\|\[\]\/\\]+\.)*(?<domain>[^\s~`!@#$%^&*()_+=,.?:;'"{}\|\[\]\/\\]+)(?<tld>\.[^\s~`!@#$%^&*()\-_+=,.?:;'"{}\|\[\]\/\\0-9]{2,})))+(?<port>:\d+)?(?:\/(?<path>\/?[^\s`@#$^&=.?"{}\\]+\/)*(?<file>[^\s`@#$^&=?"{}\/\\]+)?(?<query>\?[^\s`#$^"{}\\]+)*(?<fragment>#[^\s`$^&=?"{}\/\\]+)?)?"##
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
# Quick Example (full)
https://uSer:pass@sub1.sub-2.EXamPle.uk.com:80/PATH1/paTh2?qUeRy=1&q2=2#ancHor
https://úser@suß.öctúvt.tìd/file-path/FILE_%82%8A.php?query=value&query2=value2#somethig
HTTP://دامنه.ایران/زیردامنه/زیردامنه۲/فایل.پسوند؟درخواست=جواب
http://blog.sergeys.us/beer?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+SergeySus+(Sergey+Sus+Photography+%C2%BB+Blog)&utm_content=Google+Reader
http://test.site/wp-admin/post.php?t=1347548645469?t=1347548651124?t=1347548656685?t=1347548662469?t=1347548672300?t=1347548681615?#sdfsdf
http://example.com/wp-admin/load-scripts.php?c=1&load[]=swfobject,jquery,utils&ver=3.5
http://video.google.co.uk:80/videoplay?docid=-7246927612831078230&hl=en#hello
https://example.com/path/resource.txt#fragment
# Basic
www.example.com
http://example.com
https://example.com
https://example.ac.eu
https://example.gov.ir
ftp://example.com
sftp://example.com
# With Path
www.example.com/path1/path2/
http://example.com/path1/path2/
https://example.com/path1/path2
# With Path & File
www.example.com/path1/path2/file.pdf
http://example.com/path1/path2/file.c
https://example.com/path1/path2/file.txt
# With Path & Query
http://www.example.com/?query=some-path/path1/path2/
http://example.com/path1/path2/?query=something
https://example.com/path1/path2?query=something
# With Subdomain
sub1.sub2.example.com
www.example.com
http://www.example.com
ftp://sub1.example.com
# With Port
www.example.com:80
http://example.com:8080
ftp://example.com:21
ftp://sub1.example.com:21
http://example.com:8080/path1/path2/
http://example.com:80/path1/path2/file.c
http://example.com:443/path1/path2/?query=something
# With Username
user@example.com
http://user@www.example.com
ftp://user@example.com
ftp://user@sub1.example.com:21
http://user@example.com:8080/path1/path2/
http://user@example.com/path1/path2/file.c
http://user@example.com/path1/path2/?query=something
# Some Crazy Examples
http://ヒキワリ.ナットウ.ニホン
sip://ßàÁâãóôþüúðæåïçèõöÿýòäœêëìíøù.îûñé
HTTP://دامنه.ایران/زیردامنه/زیردامنه۲/فایل.پسوند؟درخواست=جواب
http://example.com/wp-admin/load-scripts.php?c=1&load[]=swfobject,jquery,utils&ver=3.5
https://úser@sub1.suß2.sub3.öctúvt.tìd.tìd/V3/file-path/P----29BSx_A_%82%8A_D_M1n_a.php?query=value#somethig
http://www.go.com.au/ersdfs?dfd=dfgd@s=1
https://uSer@EXamPle.uk.com:80/?qyery?qUeRy=1&q2=2#ancHor
http://example.com/%E5%BC%95%E3%81%8D%E5%89%B2%E3%82%8A.html
hTTp://eXamPLE.CoM
https://belgië.be
https://belgië.be.gov
http://user@[2001:db8::1]/
https://user@111.222.33.44:49171/Cda-cgi/clientcgi?action=start
http://[2001:db8::1]:80
a2d.dd4d.d5d.www.go.com.au
a.b.c.d.e.go.com.co.uk
go.com.uk
go.go.com
1go-co.com
1-2-3-4--5-go.com
www.go.com.au
# These Should not be Seen as Domain
example.c
example..com
123.b
go.12
1.2.3.4
ff:ff:ff:ff
C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe
hTTps://111.222.533.44:49171/cda-cgi/clientcgi?action=start
# These Should be Seen Partially
//example.com.c
example.com.c
http://example.com.com.
# It Should Find URLs in a Text too
create Share Links to send your expressions to co-workers or link to them on Twitter or your blog [ex. http://RegExr.com?2rjl655]
Built by gskinner.com with Flex 3 [adobe.com/go/flex] and Spelling Plus Library for text highlighting[gskinner.com/products/spl].
# Resources
- https://en.wikipedia.org/wiki/Uniform_Resource_Identifie
- https://en.wikipedia.org/wiki/List_of_URI_schemes
-
- https://zencoder.support.brightcove.com/general-information/special-characters-usernames-and-passwords.html
- https://support.microsoft.com/en-us/topic/certain-special-characters-are-not-allowed-in-the-url-entered-into-the-address-bar-of-internet-explorer-a8e2a966-19d6-27af-06cc-e720f25e8b02
- https://perishablepress.com/stop-using-unsafe-characters-in-urls/
- https://help.dragonmetrics.com/en/articles/213986-invalid-characters-in-url
"""##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression