如何通过 Swift3 将 HTML 字符串分隔成数组或字典?

How to separate HTML string into array or dictionary by Swift3?

我从 API 中得到 HTML 字符串,如下所示:

let a: String = "<a href="https://www.google.com.tw">https://www.google.com.tw </a>"
let b: String = "<a href="myAppName://app/user/aa3b77411825b88b318d77gg">@Tim </a>Hello Tim"
let c: String = "<a href="myAppName://app/user/aa3b77411825b88b318d77gg">@Tim </a><a href="https://www.google.com.tw">https://www.google.com.tw </a>"

let splitedArray1: [String] = a.componentsSeparatedByString("?????") //splited string which is the best 
let splitedArray2: [String] = b.componentsSeparatedByString("?????") //splited string which is the best
let splitedArray3: [String] = c.componentsSeparatedByString("?????") //splited string which is the best

我想把link和他们分开,得到如下数据

print(splitedArray1) //["https://www.google.com.tw","https://www.google.com.tw"]
print(splitedArray2) //["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","Hello Tim"]
print(splitedArray3) //["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","https://www.google.com.tw","https://www.google.com.tw "]

没有库的简单解决方案 - 只需使用 String.replaceOccurences(of:... 将 href、a 等奇数字符串替换为拆分参数(如“|”),然后使用 componentsSeparatedByString("|")获取您的组件。

可能的解决方案:使用NSAttributedString然后在NSLinkAttributeName上枚举,如果没有,说明没有link标签,所以你只需要保留[=22] =],否则,您添加 link,然后添加字符串。

在 Playground 中快速编写:

let a: String = "<a href=\"https://www.google.com.tw\">https://www.google.com.tw </a>"
let b: String = "<a href=\"myAppName://app/user/aa3b77411825b88b318d77gg\">@Tim </a>Hello Tim"
let c: String = "<a href=\"myAppName://app/user/aa3b77411825b88b318d77gg\">@Tim </a><a href=\"https://www.google.com.tw\">https://www.google.com.tw </a>"

let values:[String] = [a, b, c]



for aHTMLString in values
{
    let attributedString = try! NSAttributedString.init(data: aHTMLString.data(using: .utf8)!,
                                                        options: [.documentType: NSAttributedString.DocumentType.html],
                                                        documentAttributes: nil)
    var retValues = [String]()
    attributedString.enumerateAttribute(.link,
                                        in: NSRange(location: 0, length: attributedString.string.count),
                                        options: [],
                                        using: { (attribute, range, pointerStop) in
                                            if let attribute = attribute as? URL
                                            {
                                                retValues.append(attribute.absoluteString)
                                            }
                                            let subString = (attributedString.string as NSString).substring(with: range)
                                            retValues.append(subString)
    })

    print("*** retValues: \(retValues)")
}

let targetResult1 = ["https://www.google.com.tw","https://www.google.com.tw"]
let targetResult2 = ["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","Hello Tim"]
let targetResult3 = ["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","https://www.google.com.tw","https://www.google.com.tw "]
print("targetResult1: \(targetResult1)")
print("targetResult2: \(targetResult2)")
print("targetResult3: \(targetResult3)")

输出:

*** retValues: ["https://www.google.com.tw/", "https://www.google.com.tw "]
*** retValues: ["myappname://app/user/aa3b77411825b88b318d77gg", "@Tim ", "Hello Tim"]
*** retValues: ["myappname://app/user/aa3b77411825b88b318d77gg", "@Tim ", "https://www.google.com.tw/", "https://www.google.com.tw "]
targetResult1: ["https://www.google.com.tw", "https://www.google.com.tw"]
targetResult2: ["myAppName://app/user/aa3b77411825b88b318d77gg", "@Tim ", "Hello Tim"]
targetResult3: ["myAppName://app/user/aa3b77411825b88b318d77gg", "@Tim ", "https://www.google.com.tw", "https://www.google.com.tw "]

有细微差别,我复制了你的 "target" (splitArray),最后一个缺少 space,我的代码倾向于在 [= 上添加最后一个“/” 24=]s.

我创建了此扩展程序以获得 url。

extension String {
  func getUrl() -> String? {
      let rss = self.split { (char) -> Bool in
          return char == ">"
      }
      if let final = rss.last?.split(separator: "<"), let first = final.first {
          return String(first)
      }
      return nil
  }

  var hrefUrl: String {
    let matchString = "=\""
    let arrComponents = self.components(separatedBy: matchString)
    if let first = arrComponents.last, let str = first.split(separator: "\"").first {

        return String(str)
    }
    return ""
  }
}

用法:

let a: String = "<a href=\"https://www.google.com.tw\">https://www.google.com.tw </a>"
a.getUrl()  //output: https://www.google.com.tw 

//or

a.hrefUrl //output: https://www.google.com.tw 

使用正则表达式提取URL。下面我写了代码片段。

        let text = "<a href=\"https://www.google.com\">"

        let regex = try! NSRegularExpression(pattern: "<a[^>]+href=\"(.*?)\"[^>]*>")
        let range = NSMakeRange(0, text.characters.count)
        let matches = regex.matches(in: text, range: range)
        for match in matches {
            let strURL = (text as NSString).substring(with: match.rangeAt(1))
            print(strURL)
        }