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