user3127109 user3127109 - 1 year ago 90
Swift Question

How to get only link from this string in swift

I want to get only link from this string. Example:

"<p><a href=\"\"></a></p>\n"

I want output as

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 { nsString.substring(with: $0.range)}
} catch let error {    

And tried this regex:

But still I can't figure it out.

Answer Source

Try below code please:

let string = "<p><a href=\"\"></a></p>\n"
let str = string.stringByReplacingOccurrencesOfString("<[^>]+>", withString: "", options: .RegularExpressionSearch, range: nil)
print("string: \(str)")


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download