Reputation: 3763
I want to get only the link from this string:
"<p><a href=\"https://www.youtube.com/watch?v=i2yscjyIBsk\">https://www.youtube.com/watch?v=i2yscjyIBsk</a></p>\n"
I want output as https://www.youtube.com/watch?v=i2yscjyIBsk
So, how I can I achieve it?
I have tried:
func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let nsString = text as NSString
let results = regex.matches(in: text, range: NSRange(location: 0, length: nsString.length))
return results.map { nsString.substring(with: $0.range)}
} catch let error {
}
And tried this regex: "<a[^>]+href=\"(.*?)\"[^>]*>.*?</a>"
But still I can't figure it out.
Upvotes: 2
Views: 3350
Reputation: 4096
By using NSDataDetector
class you can extract links exactly:
let text = "<p><a href=\"https://www.youtube.com/watch?v=i2yscjyIBsk\">https://www.youtube.com/watch?v=i2yscjyIBsk</a></p>\n"
let types: NSTextCheckingType = .Link
let detector = try? NSDataDetector(types: types.rawValue)
guard let detect = detector else {
return
}
let matches = detect.matchesInString(text, options: .ReportCompletion, range: NSMakeRange(0, text.characters.count))
for match in matches {
print(match.URL!)
}
Description: NSDataDetector
class can match dates, addresses, links, phone numbers and transit information. Reference.
The results of matching content is returned as NSTextCheckingResult
objects. However, the NSTextCheckingResult
objects returned by NSDataDetector
are different from those returned by the base class NSRegularExpression
.
Results returned by NSDataDetector
will be of one of the data detectors types, depending on the type of result being returned, and they will have corresponding properties. For example, results of type date have a date, timeZone
, and duration; results of type link have a url
, and so forth.
There is another way to get link and other specific string between <a> ... </a>
tag:
let string = "<p><a href=\"https://www.youtube.com/watch?v=i2yscjyIBsk\">https://www.youtube.com/watch?v=i2yscjyIBsk</a></p>\n"
let str = string.stringByReplacingOccurrencesOfString("<[^>]+>", withString: "", options: .RegularExpressionSearch, range: nil)
print("string: \(str)")
Output:
string: https://www.youtube.com/watch?v=i2yscjyIBsk
Note: I suggest you to use above solution to get the links specifically thanks.
Upvotes: 9