gpt4 book ai didi

regex - Swift 中特定字符后用空格分隔值

转载 作者:行者123 更新时间:2023-11-30 10:01:16 25 4
gpt4 key购买 nike

嗨,我有一个值是

Blockquote 1 1:0.0644343 1 1:0.0309334 1 1:0.0261616 Blockquote

我想用空格分隔值,但在某些字符之后得到这样的结果..有没有可能的解决方案。我知道我们可以用正则表达式来做

Blockquote "1 1:0.0644343" "1 1:0.0309334" "1 1:0.0261616" Blockquote

最佳答案

我认为正则表达式是这里的完美工具:

var str = "Blockquote 1 1:0.0644343 1 1:0.0309334 1 1:0.0261616 Blockquote"

let regex = try! NSRegularExpression(pattern: "(\\d \\d:[\\.0-9]+)", options: [])
let matches = regex.matchesInString(str, options: [], range: NSMakeRange(0, str.characters.count))

for m in matches.reverse() {
let range = m.rangeAtIndex(1)
let startIndex = str.startIndex.advancedBy(range.location)
let endindex = startIndex.advancedBy(range.length)
let value = str[startIndex..<endindex]

str.replaceRange(startIndex..<endindex, with: "\"\(str[startIndex..<endindex])\"")
}

print(str)

关于regex - Swift 中特定字符后用空格分隔值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38386000/

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