I need to extract data from a string which is returned from a website with POST request; I am parsing the data with SwiftSoup library. I have selected the list items using a CSS selector:
let iconsList: Element = try doc.select("ul.icons-list").first()!
which returns html like this:
<ul class="icons-list">
<li><strong>Label 1:</strong> Value 1 (Some text) </li>
<li><strong>Label 2:</strong> Value 2</li>
<li><strong>Label 3:</strong> Value 3</li>
<li><strong>Label 4:</strong> Value 4 </li>
<li><strong>Label 5:</strong> Value 5</li>
</ul>
Now I need to extract labels and values and store inside array or maybe separate variables. I have tried Regex like shown below (didn't work, maybe wrong regex):
let result = "This <strong>Needs to be removed</strong> is my string"
let regex = try! NSRegularExpression(pattern: "<strong>(.*)</strong>", options: .caseInsensitive)
var newStr = regex.stringByReplacingMatches(in: result, options: [], range: NSRange(0..<str.utf16.count), withTemplate: "")
print(newStr)
And also tried SwiftSoup selector like:
var labelFirst = try doc.select("ul.icons-list li:nth-child(1)")
But it also returns HTML result. So, I need to use regex in both cases. How can this be done?
Another Question: When I select icons-list class using SwiftSoup ".select" selector. If there's an exception then how do I handle that? Currently, I have this code but it's not working. And what if I want to handle multiple try blocks inside this block?
do{
let doc: Document = try SwiftSoup.parse(responseString!)
let iconsList: Element = try doc.select("ul.icons-list").first()!
print(iconsList)
}catch Exception.Error( _, let message){
print("icons list not found "+message)
}catch{
print("error")
}