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 = "[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 ???
I want to get the Capital Character, then replace them to -(the Capital character's lower character)-
, but my regExp's result is console 12 count of Capital Characters.
1) You see, it should be 3, where is the mistake ?
2) How to replace the Capital character to -(the capital's lower character)-
?
Addition -1
There are all of the NSRegularExpression.Options
below:
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 }`
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
Regarding your second criteria, using a regex group will cover that:
For something more advanced, where you need additional custom code for each match: