gpt4 book ai didi

regex - 如何获取包含属于数组的单词的字符串的开头?

转载 作者:搜寻专家 更新时间:2023-11-01 05:34:44 25 4
gpt4 key购买 nike

给定要查找的单词列表(及其复数):

let array = ["\\b(ab+?)(s\\b|\\b)", "\\b(cd+?)(s\\b|\\b)", "\\b(ef+?)(s\\b|\\b)", "\\b(gh+?)(s\\b|\\b)"]

这是以下的缩写:

let words = ["ab", "cd", "ef", "gh"]
let array = words + words.map {$0 + "s"}

我想从一个句子中获取所有单词。从字符串的开头开始,直到满足正则表达式的最后一个单词array 中的每个元素或其复数形式都应该匹配(通过添加“s”,ies 不考虑复数形式):

例如:

let string_1 = "abc ab def cds ghi jkl bs mno" // should get "abc ab def cds"
let string_2 = "abc ghs def" // should get "abc ghs"
let string_3 = "abc ab def bc ghi" // should get "abc ab"
let string_4 = "abc def" // should get "" or nil

我目前的想法是:

let words = string_1.components(separatedBy: CharacterSet.whitespacesAndNewlines)
let lastOccurrence: (Int, String)? = words.enumerated().reversed().first(where: { (index, value) in
if let _ = restOfSentence.range(of: value, options: .regularExpression) {
return true
}
return false
})

if let l_o = lastOccurrence {
let matchingWordsArray = words[0...l_o.0]
let matchingWords = matchingWordsArray.joined(separator: " ")
print(matchingWords)
}

将一个句子分解成单词然后加入它们对我来说似乎很昂贵。
有一个更好的方法吗?

最佳答案

你可以使用 NSRegularExpression(虽然我发现它的 Swift 实现令人讨厌地充满了旧的 NSRange/NSString 依赖):

let string_1 = "abc ab def cds ghi jkl bs mno" // should get "abc ab def cds"
let string_2 = "abc ghs def" // should get "abc ghs"
let string_3 = "abc ab def bc ghi" // should get "abc ab"
let string_4 = "abc def" // should get "" or nil


let array = ["\\b(ab+?)(s\\b|\\b)", "\\b(cd+?)(s\\b|\\b)", "\\b(ef+?)(s\\b|\\b)", "\\b(gh+?)(s\\b|\\b)"]

func matchingWords(_ string:String, patterns:[String]) -> String
{
let anyPattern = patterns.map{"("+$0+")"}.joined(separator:"|")

let string = NSString(string:string)
let fullRange = NSMakeRange(0,string.length)

if let regEx = try? NSRegularExpression(pattern:anyPattern, options:.caseInsensitive),
let lastRange = regEx.matches(in:string as String, range:fullRange).last?.range
{
return string.substring(to: lastRange.location + lastRange.length)
}

return ""
}

print( matchingWords( string_1, patterns:array) ) // abc ab def cds
print( matchingWords( string_2, patterns:array) ) // abc gh
print( matchingWords( string_3, patterns:array) ) // abc ab
print( matchingWords( string_4, patterns:array) ) // <empty string>

关于regex - 如何获取包含属于数组的单词的字符串的开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43045612/

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