Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does NSRegularExpression support partial case insensitive?

As the title states, I wonder if NSRegularExpression in both Objective-c or Swift, support partial case insensitive search?

Namely, will the pattern recognize (?ismx)? If not, is there a brief reason for this inability?

I truly appreciate your explanation.

like image 431
donkey Avatar asked Mar 17 '23 23:03

donkey


1 Answers

From the NSRegularExpression Class Reference:

Table 2 Regular Expression Operators

...

(?ismwx-ismwx:...)
Flag settings. Evaluate the parenthesized expression with the specified flags enabled or -disabled. ...

(?ismwx-ismwx)
Flag settings. Change the flag settings. Changes apply to the portion of the pattern following the setting. For example, (?i) changes to a case insensitive match. ...

Example:

let pattern = "(?i)f(?-i)oo"
//Or: let pattern = "(?i:f)oo"
let regex = NSRegularExpression(pattern: pattern, options: nil, error: nil)!

let string : NSString = "foo, Foo, fOO"
regex.enumerateMatchesInString(string, options: nil, range: NSMakeRange(0, string.length)) {
    (result, flags, stop) -> Void in
    println(string.substringWithRange(result.range))
}

Output:

foo
Foo

The pattern matches "foo" and "Foo" because the "f" is matched case insensitive. It does not match "fOO" because "oo" is matched case sensitive.

like image 53
Martin R Avatar answered Apr 06 '23 21:04

Martin R