RegExp result is not my expected

前端 未结 1 1624
天命终不由人
天命终不由人 2021-01-26 07:03

My regExp do not get a correct result, I can not find where is mistake.

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

    let pattern = \         


        
1条回答
  •  一整个雨季
    2021-01-26 07:42

    You need to do a case-sensitive match. Change options from .caseInsensitive to [].

    NSRegularExpression.Options is an OptionSet, which allows specifying zero or more values. When passing zero, or more then one, an array literal syntax is used. See: 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")
    

    Regarding your second criteria, using a regex group will cover that:

    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)")
    

    For something more advanced, where you need additional custom code for each match:

    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)")
    

    0 讨论(0)
提交回复
热议问题