Does NSRegularExpression support partial case insensitive?

↘锁芯ラ 提交于 2019-12-05 18:21:07

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!