gpt4 book ai didi

ios - RegExp结果不是我的预期

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

我的regExp没有得到正确的结果,我找不到错误所在。

    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 ???


我想获取大写字母,然后将其替换为 -(the Capital character's lower character)-,但是我的regExp的结果是控制台12的大写字母计数。

1)您知道,应该是3,错误在哪里?
2)如何将大写字符替换为 -(the capital's lower character)-



加法-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/57973031/

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