iOS開發之常用的正規表示式

2021-07-10 22:59:56 字數 3860 閱讀 2301

在 ios 中,我們使用 nspredicate 的字串比較功能來進行正規表示式處理,其比較關鍵字為:matches

下面,列舉乙個匹配6-15個由字母/數字組成的字串的正規表示式,來看看 nspredicate 的具體使用:

nsstring * regex        = @"(^[a-za-z0-9]$)";  

nspredicate * pred = [nspredicate predicatewithformat:@"self matches %@", regex];

bool ismatch = [pred evaluatewithobject:@"123456abcde"];

下面是一些常用的正規表示式

//郵箱

+ (bool) validateemail:(nsstring *)email

";

nspredicate *emailtest = [nspredicate predicatewithformat:@"self matches %@", emailregex];

return [emailtest evaluatewithobject:email];

}

//手機號碼驗證

+ (bool) validatemobile:(nsstring *)mobile

$";

nspredicate *phonetest = [nspredicate predicatewithformat:@"self matches %@",phoneregex];

return [phonetest evaluatewithobject:mobile];

}

//車牌號驗證

+ (bool) validatecarno:(nsstring *)carno

[a-za-z][a-za-z_0-9][a-za-z_0-9_\u4e00-\u9fa5]$";

nspredicate *cartest = [nspredicate predicatewithformat:@"self matches %@",carregex];

nslog(@"cartest is %@",cartest);

return [cartest evaluatewithobject:carno];

}

//車型

+ (bool) validatecartype:(nsstring *)cartype

//使用者名稱

+ (bool) validateusername:(nsstring *)name

+$";

nspredicate *usernamepredicate = [nspredicate predicatewithformat:@"self matches %@",usernameregex];

bool b = [usernamepredicate evaluatewithobject:name];

return b;

}

//密碼

+ (bool) validatepassword:(nsstring *)password

+$";

nspredicate *passwordpredicate = [nspredicate predicatewithformat:@"self matches %@",passwordregex];

return [passwordpredicate evaluatewithobject:password];

}

//暱稱

+ (bool) validatenickname:(nsstring *)nickname

$";

nspredicate *passwordpredicate = [nspredicate predicatewithformat:@"self matches %@",nicknameregex];

return [passwordpredicate evaluatewithobject:nickname];

}

//身份證號

+ (bool) validateidentitycard: (nsstring *)identitycard

nsstring *regex2 = @"^(\\d|\\d)(\\d|[xx])$";

nspredicate *identitycardpredicate = [nspredicate predicatewithformat:@"self matches %@",regex2];

return [identitycardpredicate evaluatewithobject:identitycard];

}

其實ios中有三種方式來實現正規表示式的匹配。現在將他們都記錄在這裡:

1.利用nspredicate(謂詞)匹配

nsstring *email = @「[email protected]」;

nsstring

*regex = @"[a-z0-9a-z._%+-]+@[a-za-z0-9.-]+\\.[a-za-z]";

nspredicate *predicate = [nspredicate

predicatewithformat:@"self matches %@", regex];

bool isvalid = [predicate evaluatewithobject:email];

謂詞匹配比較靈活,但是需要有謂詞的相關知識。

2.利用rangeofstring:option:直接查詢

nsstring *searchtext = @"// do any additional setup after loading the view, typically from a nib.";

nsrange range = [searchtext rangeofstring:@"(?:[^,])*\\." options:nsregularexpressionsearch];

if (range.location != nsnotfound)

options中設定nsregularexpressionsearch就是表示利用正規表示式匹配,會返回第乙個匹配結果的位置。

3.使用正規表示式類

nsstring *searchtext = @"// do any additional setup after loading the view, typically from a nib.";    

nserror *error = null;

nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:@"(?:[^,])*\\." options:nsregularexpressioncaseinsensitive error:&error];

nstextcheckingresult *result = [regex firstmatchinstring:searchtext options:0 range:nsmakerange(0, [searchtext length])];

if (result)

使用系統的正規表示式類(nsregularexpression)會返回匹配的多個結果。

小結:第一種匹配需要學習nspredicate的寫法,需要查閱蘋果相關技術文件;如果只關心第乙個匹配的結果,第二種匹配較為簡潔;如果需要匹配多個結果,同時匹配多次,第三種方式效率會更高

iOS開發之常用正規表示式

正規表示式是一種用來進行文字匹配的工具,其語法優美簡潔,通過正規表示式我們將這些業務描述成某些需求規則,來讓我們的 更美觀 實用.下面是一些常用的正規表示式 1.驗證使用者輸入的密碼長度是否滿足6 18位的長度,最常見的驗證方式是判斷輸入的密碼長度 2.固定 都是0區號 八位數字的格式 func i...

正規表示式 開發常用的正規表示式

正規表示式是一種用來進行文字匹配的工具,其語法優美簡潔。在開發中,查詢 對比以及匹配字串是家常便飯的業務,通過正規表示式我們將這些業務描述成某些需求規則,來讓我們的 更美觀 實用。例如我們要驗證使用者輸入的密碼長度是否滿足6 18位的長度,新手最常見的驗證方式是判斷輸入的密碼長度 return te...

正規表示式 iOS開發

a.正規表示式 正規表示式使 用單個字串來描述 匹配 一系列符合某個句法規則的字串。在很多 文字編輯器 裡,正規表示式通常被 用來檢索 替換那些 符合某個模式的 文字。b.原義 文字字元 正常的 文字字元。c.元字元 具有特殊意義的專 用字元,是代替正常 文字字元的字元。d.限定符 限定匹配的數量或...