使用iOS中的正则表达式进行验证

时间:2015-09-04 10:21:02

标签: ios regex validation xcode6 nsregularexpression

我正在iOS中使用正则表达式(Regex)实现用户名字段的验证。我不希望用户名以“guest”开头。我试过下面的代码,但它没有用。

[txtUserName addRegx:@"^(guest)" withMsg:@"Username Can't start with the word guest"];

想法?

2 个答案:

答案 0 :(得分:2)

您可以尝试使用此正则表达式:

^(?!guest).*$

说明:

^ 在字符串开头处断言位置

(?!guest)否定前瞻 - 断言无法匹配下面的正则表达式 客人字面匹配字符客户(区分大小写)

。* 匹配任何字符(换行符除外)

量词:*在零和无限次之间,尽可能多次,根据需要回馈[贪心]

$ 在字符串末尾断言位置

修改

要使其不区分大小写,您可以尝试:

^(?i:(?!guest)).*$

答案 1 :(得分:0)

您必须删除(),如下所示:

[txtUserName addRegx:@"^guest" withMsg:@"Username Can't start with the word guest"];