Reputation: 837
Is it possible to take just a part of the HTML string that I have obtained passing the URL?
Example code below:
let myURLString = "https://myUrl.something"
guard let myURL = NSURL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return
}
do {
let myHTMLString = try String(contentsOf: myURL as URL)
let htmlString = String(myHTMLString)
print("HTML: \(myHTMLString)")
} catch let error as NSError {
print("Error: \(error)")
}
I want to take what's inside the tag <h3 class="post-title">
to </h3>
.
I know that I should use the regular expressions but I don't really know how to set it in the right way. I tried something like this:
let myURLString = "https://www.fvgtech.it/category/podcast/"
guard let myURL = NSURL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return
}
do {
let myHTMLString = try String(contentsOf: myURL as URL)
let htmlString = String(myHTMLString)
if let match = htmlString.range(of: "(<h3.+)", options: .regularExpression) {
print("Found",htmlString.substring(with: match))
}
print("HTML: \(myHTMLString)")
} catch let error as NSError {
print("Error: \(error)")
}
But it's printing just <h3 class="post-title">
and not what's in the middle. Thanks in advance!
Upvotes: 1
Views: 1719
Reputation: 6067
Just we need to search all substrings between start String and end String See Extension of String
let myURLString = "https://www.fvgtech.it/category/podcast/"
guard let myURL = NSURL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return
}
do {
let myHTMLString = try String(contentsOf: myURL as URL)
let htmlString = String(myHTMLString)
print(htmlString.allStringsBetween("<h3 class=\"post-title\">", andString: "</h3>"))
} catch let error as NSError {
print("Error: \(error)")
}
Extension for String
extension String{
func allStringsBetween(start: String, end: String) -> [Any] {
var strings = [Any]()
var startRange: NSRange = (self as NSString).range(of: start)
while true {
if startRange.location != NSNotFound {
var targetRange = NSRange()
targetRange.location = startRange.location + startRange.length
targetRange.length = self.count - targetRange.location
let endRange: NSRange = (self as NSString).range(of: end, options: [], range: targetRange)
if endRange.location != NSNotFound {
targetRange.length = endRange.location - targetRange.location
strings.append((self as NSString).substring(with: targetRange))
var restOfString = NSRange()
restOfString.location = endRange.location + endRange.length
restOfString.length = self.count - restOfString.location
startRange = (self as NSString).range(of: start, options: [], range: restOfString)
}
else {
break
}
}
else {
break
}
}
return strings
}
}
Upvotes: 3