gpt4 book ai didi

ios - RegExp 结果不是我的预期

转载 作者:行者123 更新时间:2023-11-28 15:59:26 24 4
gpt4 key购买 nike

我的正则表达式没有得到正确的结果,我找不到错误的地方。

    let ori_str = "abcXabcYabcZ"  // there are 3 Capital character

let pattern = "[A-Z]"

let regular = try!NSRegularExpression(pattern: pattern, options: .caseInsensitive)

let results = regular.matches(in: ori_str, options: .reportProgress , range: NSMakeRange(0, ori_str.characters.count))
print("results have: \(results.count) count") // log 'result have: 12 count' why 12 without 3 ???

我想获取大写字符,然后将它们替换为 -(大写字符的小写字符)-,但我的正则表达式的结果是 console 12 count of Capital Characters。

1)你看,应该是3,哪里错了?
2) 如何将大写字母替换为-(大写字母的小写字母)- ?


加法-1

下面是所有的 NSRegularExpression.Options:

    public static var caseInsensitive: NSRegularExpression.Options { get }

public static var allowCommentsAndWhitespace: NSRegularExpression.Options { get }

public static var ignoreMetacharacters: NSRegularExpression.Options { get }

public static var dotMatchesLineSeparators: NSRegularExpression.Options { get }

public static var anchorsMatchLines: NSRegularExpression.Options { get }

public static var useUnixLineSeparators: NSRegularExpression.Options { get }

public static var useUnicodeWordBoundaries: NSRegularExpression.Options { get }`

最佳答案

您需要进行区分大小写的匹配。将选项从 .caseInsensitive 更改为 []。

NSRegularExpression.Options 是一个 OptionSet,它允许指定零个或多个值。当传递零或多于一时,使用数组文字语法。请参阅:https://developer.apple.com/reference/swift/optionset

let ori_str = "abcXabcYabcZ"  // there are 3 Capital character
let pattern = "[A-Z]"
let regular = try!NSRegularExpression(pattern: pattern, options: [])
let results = regular.matches(in: ori_str, options: .reportProgress, range: NSMakeRange(0, ori_str.characters.count))
print("results have: \(results.count) count")

关于您的第二个标准,使用正则表达式组将涵盖:

let ori_str = "abcXabcYabcZ"  // there are 3 Capital character
let pattern = "([A-Z])"
let regular = try!NSRegularExpression(pattern: pattern)
let replaced = regular.stringByReplacingMatches(
in: ori_str, options: [],
range: NSMakeRange(0, ori_str.characters.count),
withTemplate: "-$1-").lowercased()
print("Uppercase characters replaced: \(replaced)")

对于更高级的内容,您需要为每个匹配添加额外的自定义代码:

extension String {
func replace(regex: NSRegularExpression, with replacer: (_ match:String)->String) -> String {
let str = self as NSString
let ret = str.mutableCopy() as! NSMutableString

let matches = regex.matches(in: str as String, options: [], range: NSMakeRange(0, str.length))
for match in matches.reversed() {
let original = str.substring(with: match.range)
let replacement = replacer(original)
ret.replaceCharacters(in: match.range, with: replacement)
}
return ret as String
}
}

let ori_str = "abcXabcYabcZ" // there are 3 Capital character
let pattern = "[A-Z]"
let regular = try!NSRegularExpression(pattern: pattern)
let replaced = ori_str.replace(regex: regular) { "-\($0.lowercased())-" }
print("Uppercase characters replaced: \(replaced)")

关于ios - RegExp 结果不是我的预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41294709/

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