NSRegularExpression 是否支持部分不区分大小写? [英] Does NSRegularExpression support partial case insensitive?

查看:108
本文介绍了NSRegularExpression 是否支持部分不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我想知道 Objective-c 或 Swift 中的 NSRegularExpression 是否支持部分不区分大小写的搜索?

也就是说,模式会识别 (?ismx) 吗?如果没有,是否有造成这种无能的简短原因?

非常感谢您的解释.

解决方案

来自 NSRegularExpression 类参考:

<块引用>

表 2 正则表达式运算符

...

(?ismwx-ismwx:...)
标志设置.计算启用或禁用指定标志的括号表达式....

(?ismwx-ismwx)
标志设置.更改标志设置.更改适用于设置后的图案部分.例如, (?i) 更改为不区分大小写的匹配项....

示例:

let pattern = "(?i)f(?-i)oo"//或: let pattern = "(?i:f)oo"let regex = NSRegularExpression(pattern: pattern, options: nil, error: nil)!让字符串:NSString = "foo, Foo, fOO"regex.enumerateMatchesInString(string, options: nil, range: NSMakeRange(0, string.length)) {(结果,标志,停止)->作废println(string.substringWithRange(result.range))}

输出:

<前>富富

模式匹配foo"和Foo",因为f"匹配不区分大小写.它不匹配fOO",因为oo"区分大小写.

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.

解决方案

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.

这篇关于NSRegularExpression 是否支持部分不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆