import Foundation
let pattern = #"(?:https:\/\/goo\.gl\/maps\/\w+)|(?:https:\/\/www\.google\.com\/maps[^ ]*\d)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
https://goo.gl/maps/WwcXqVEA5vrEWFtAA
follow this link https://goo.gl/maps/rob6c2k27E4ZpiDd8
https://goo.gl/maps/efNxki1ek7RZ3CAFA need to check in google maps
https://www.google.com/maps/@-6.2964394,107.1813443,3a,75y,90t/data=!3m8!1e2!3m6!1sAF1QipNrLqMTMz-iqWjqvmedTHyQwGLhBE-Z0L46Z5mY!2e10!3e12!6shttps:%2F%2Flh5.googleusercontent.com%2Fp%2FAF1QipNrLqMTMz-iqWjqvmedTHyQwGLhBE-Z0L46Z5mY%3Dw203-h152-k-no!7i1000!8i750 is correct?
https://www.google.com/maps/place/Jl.+Raya+Rw.+Bangkong,+Sertajaya,+Kec.+Cikarang+Tim.,+Bekasi,+Jawa+Barat+17530/@-6.2971014,107.184237,19z/data=!3m1!4b1!4m5!3m4!1s0x2e699b50084e936d:0xc3f7ba2bef9eafc8!8m2!3d-6.2971641!4d107.1847831 how about this? 1234
https://google.com/maps/place/Jl.+Raya+Rw.+Bangkong,+Sertajaya,+Kec.+Cikarang+Tim.,+Bekasi,+Jawa+Barat+17530/@-6.2971014,107.184237,19z/data=!3m1!4b1!4m5!3m4!1s0x2e699b50084e936d:0xc3f7ba2bef9eafc8!8m2!3d-6.2971641!4d107.1847831 how about this? 1234
"""#
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