gpt4 book ai didi

swift - 找到一个字符串并提取后面的字符

转载 作者:行者123 更新时间:2023-11-30 12:30:58 26 4
gpt4 key购买 nike

用户提交信息后,我正在访问网页的 HTML。

如果我有...

var htmlString = "This is the massive HTML string - it has lots of characters "FindStringDetails"<123789456.123456>  This is a massive HTML string - "
var findString = "FindStringDetails\"<"

有没有合适的方法可以提取“FindStringDetails”之后的数字<给我123789456.123456?

最佳答案

import Foundation

var htmlString = "This is the massive HTML string - it has lots of characters \"FindStringDetails\"<123789456.123456> This is a massive HTML string - "
var findString = "FindStringDetails\"<"

extension String {
func matchingStrings(regex: String) -> [[String]] {
guard let regex = try? NSRegularExpression(pattern: regex, options: []) else { return [] }
let nsString = NSString(string: self)
let results = regex.matches(in: self, options: [], range: NSMakeRange(0, nsString.length))
return results.map { result in
(0..<result.numberOfRanges).map { result.range(at: $0).location != NSNotFound
? nsString.substring(with: result.range(at: $0))
: ""
}
}
}
}

let m = htmlString.matchingStrings(regex: "\(findString)(\\d+)\\.(\\d+)")

print(m[0][1]) // 123789456
print(m[0][2]) // 123456

(代码改编自here。)

关于swift - 找到一个字符串并提取后面的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43565082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com