我有这个字符串,它是一个较大字符串的一部分,多次出现“content”和“/ content”:我想捕获“content ...”和“/content".
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">RATING: ★★★★<br/>
TAGS: Fiction, General, Science Fiction<br/>
SERIES: 20 SienceFiction Greats [19]<br/>
<p class="description">SUMMARY:<br/>Luna is an open colony and the regime is a harsh one....</p></div>
</content>
let regexPattern = "<content type=\"xhtml\">.*</content>"
let result:[String] = matches(for: regexPattern, in: dataString)
但它返回一个空数组。
答案 0 :(得分:0)
你的正则表达式模式很接近。您在正则表达式中没有任何捕获组。捕获组定义为()
之间的任何模式。因此,通过对正则表达式进行一点调整,您将获得匹配。更新的Regex应如下所示:
let regexPattern = "<content type=\"xhtml\">(.*)<\/content>"
答案 1 :(得分:0)
我找到了一个解决方案,因为。*模式捕获了第一次出现的内容&#34;内容&#34;到最后一次出现&#34; / content&#34;。
所以这是我的解决方案:获取一系列&#34;内容&#34;出现和一组&#34; / content&#34;发生,从这些数组我可以从字符串
计算我需要的范围private func getXHTMLContentFromDataString(dataString: String) -> [String] {
var contentStringArray: [String] = []
let startString: String = "<content type=\"xhtml\">"
let endString: String = "</content>"
var isFound = true
var currentString = dataString
while isFound == true && currentString.characters.count > 0 {
if let contentStartRange = currentString.range(of: startString), let contentEndRange = currentString.range(of: endString) {
isFound = true
let contentStr: String = currentString[contentStartRange.upperBound .. < contentEndRange.lowerBound]
contentStringArray.append(contentStr)
currentString = currentString[contentEndRange.upperBound .. <currentString.endIndex]
} else {
isFound = false
}
}
return contentStringArray
}