Search 子字符串的快速范围-带通配符的搜索字符串
是否可以使用通配符在另一个字符串中搜索字符串? 我想查找“text1”=“text2”的匹配项,其中text1和text2可以是任何字符串 我的初始字符串是 这是我的字符串。此字符串包括“text1”=“text2”及更多内容 搜索时我不知道text1和text2。 我想到了类似于“=”的东西,但没有结果 编辑: 让我试着用另一个例子来解释。 我有*.swift文件,其中有两个locX扩展名Search 子字符串的快速范围-带通配符的搜索字符串,search,swift3,nsstring,substring,wildcard,Search,Swift3,Nsstring,Substring,Wildcard,是否可以使用通配符在另一个字符串中搜索字符串? 我想查找“text1”=“text2”的匹配项,其中text1和text2可以是任何字符串 我的初始字符串是 这是我的字符串。此字符串包括“text1”=“text2”及更多内容 搜索时我不知道text1和text2。 我想到了类似于“=”的东西,但没有结果 编辑: 让我试着用另一个例子来解释。 我有*.swift文件,其中有两个locX扩展名 labelPatternForExpresion.stringValue = "labe
labelPatternForExpresion.stringValue = "labelPatternForExpresion".locX(withComment: "comment one")
labelPath.stringValue = "labelPathToProject".locX(withComment: "comment six")
labelHeader.stringValue = "labelFileHeader".locX(withComment: "no comment")
btnFromFile.title = "btnFromFile".locX(withComment: "empty comment")
btnCancel.title = "btnCancel".locX(withComment: "")
我需要遍历该文件并找到所有对键注释:
“labelPatternForExpresion”-“注释一”
“LabelPathTopProject”-“评论六”
“btnCancel”-“假设您的模式是:
"textA" - "textB"
您需要捕获textA
和textB
。使用NSRegularExpression
:
let str = "\"labelPatternForExpresion\" - \"comment one\""
// NSRegularExpression still deals in NSString so let's make a copy to
// lessen the pain later
let nsStr = str as NSString
let regex = try! NSRegularExpression(pattern: "\"(.+)\" - \"(.+)\"", options: [])
if let match = regex.firstMatch(in: str, options: [], range: NSMakeRange(0, nsStr.length)) {
let lhs = nsStr.substring(with: match.rangeAt(1))
let rhs = nsStr.substring(with: match.rangeAt(2))
print(lhs)
print(rhs)
}
最有可能的是,swift可以像许多其他当前编程语言一样使用正则表达式。