Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only link from this string?

Tags:

regex

swift

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.

like image 859
user3127109 Avatar asked Dec 01 '25 09:12

user3127109


1 Answers

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.

like image 92
vaibhav Avatar answered Dec 04 '25 01:12

vaibhav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!