I am looking to get the content of the go:image property content out of this Japanese Web site page, with UTF-8 text encoding.
The desired result is:
http://www.macotakara.jp//blog/archives/001/201701/5871de9fb4929.jpg
But I get:
jp//blog/archives/001/201701/5871bd1be125c.jpg" />
And I believe the issue is related to the use of ranges.
You can refer to this for the regex: https://regex101.com/r/F29INt/1
The html code snippet is as follows:
<meta name="description" content="CES2017において、OtterBoxが、様々なモジュールを装着出来るモジュール式iPhoneケース「uniVERSE」の展示を行っていました。 背面にあるスライド式「uniVERSEケースシステム」を使用して、背面の下半分を変更す..." />
<meta property="og:image" name="og:image" content="http://www.macotakara.jp//blog/archives/001/201701/5871de9fb4929.jpg" />
<meta name="twitter:image"
I have my regex class as follows:
public class Regex {
let regex: NSRegularExpression
let pattern: String
public init(_ pattern: String) {
self.pattern = pattern
regex = try! NSRegularExpression(pattern: pattern, options: [.caseInsensitive])
}
public func matches(_ input: String) -> [NSTextCheckingResult] {
let matches = regex.matches(in: input, options: [], range:NSRange(location:0, length:input.characters.count))
return matches
}
}
And the code I use as follows:
let pattern = "<meta[^>]+property=[\"']\(property)[\"'][^>]+content=[\"']([^\"']*)[\"'][^>]*>"
let regex = Regex(pattern)
let matches = regex.matches(html)
for match in matches {
// range at index 0: full match
// range at index 1: first capture group
var text = ""
text += "+++StoryPreviewCache.getMetaPropertyContent(): with pattern=\(pattern) for prop=\(property)"
for j in 1..<match.numberOfRanges {
text += "+++StoryPreviewCache.getMetaPropertyContent(): Groups \(j), range=\(match.rangeAt(j)), is \(html[match.rangeAt(j)])"
}
}
print(text)
And I get:
+++StoryPreviewCache.getMetaPropertyContent():
with pattern=<meta[^>]+property=["']og:image["'][^>]+content=["']([^"']*)["'][^>]*>
for prop=og:image
+++StoryPreviewCache.getMetaPropertyContent():
Groups 1,
range=__C._NSRange,
is jp//blog/archives/001/201701/5871bd1be125c.jpg" />