iOS 正則表示式驗證
2.工程中新增libicucore.dylib frameworks。
3.現在所有的nsstring物件就可以呼叫RegexKitLite中的方法了。
NSString *email = @”[email protected]”;
[email isMatchedByRegex:@"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b”];
返回YES,證明是email格式,需要注意的是RegexKitLite用到的正則表示式和wiki上的略有區別。
regexString = @”\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?”;
NSInteger portInteger = [[searchString stringByMatching:regexString capture:1L] integerValue];
NSLog(@”portInteger: ‘%ld’”, (long)portInteger);
// 2008-10-15 08:52:52.500 host_port[8021:807] portInteger: ‘8080′
取string中http的例子。
常用正則表示式:
匹配雙位元組字元(包括漢字在內):[^x00-xff]
評註:可以用來計算字串的長度(一個雙位元組字元長度計2,ASCII字元計1)
匹配空白行的正則表示式:ns*r
評註:可以用來刪除空白行
匹配HTML標記的正則表示式:<(S*?)[^>]*>.*?|<.*? />
評註:網上流傳的版本太糟糕,上面這個也僅僅能匹配部分,對於複雜的巢狀標記依舊無能為力
匹配首尾空白字元的正則表示式:^s*|s*$
評註:可以用來刪除行首行尾的空白字元(包括空格、製表符、換頁符等等),非常有用的表示式
匹配Email地址的正則表示式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
評註:表單驗證時很實用
匹配網址URL的正則表示式:[a-zA-z]+://[^s]*
評註:網上流傳的版本功能很有限,上面這個基本可以滿足需求
匹配帳號是否合法(字母開頭,允許5-16位元組,允許字母數字下劃線):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
評註:表單驗證時很實用
匹配國內電話號碼:d{3}-d{8}|d{4}-d{7}
評註:匹配形式如 0511-4405222 或 021-87888822
匹配騰訊QQ號:[1-9][0-9]{4,}
評註:騰訊QQ號從10000開始
匹配中國郵政編碼:[1-9]d{5}(?!d)
評註:中國郵政編碼為6位數字
匹配身份證:d{15}|d{18}
評註:中國的身份證為15位或18位
匹配ip地址:d+.d+.d+.d+
評註:提取ip地址時有用
匹配特定數字:
^[1-9]d*$ //匹配正整數
^-[1-9]d*$ //匹配負整數
^-?[1-9]d*$ //匹配整數
^[1-9]d*|0$ //匹配非負整數(正整數 + 0)
^-[1-9]d*|0$ //匹配非正整數(負整數 + 0)
^[1-9]d*.d*|0.d*[1-9]d*$ //匹配正浮點數
^-([1-9]d*.d*|0.d*[1-9]d*)$ //匹配負浮點數
^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ //匹配浮點數
^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$ //匹配非負浮點數(正浮點數 + 0)
^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$ //匹配非正浮點數(負浮點數 + 0)
評註:處理大量資料時有用,具體應用時注意修正
匹配特定字串:
^[A-Za-z]+$ //匹配由26個英文字母組成的字串
^[A-Z]+$ //匹配由26個英文字母的大寫組成的字串
^[a-z]+$ //匹配由26個英文字母的小寫組成的字串
^[A-Za-z0-9]+$ //匹配由數字和26個英文字母組成的字串
^w+$ //匹配由數字、26個英文字母或者下劃線組成的字串
在使用RegularExpressionValidator驗證控制元件時的驗證功能及其驗證表示式介紹如下:
只能輸入數字:“^[0-9]*$”
只能輸入n位的數字:“^d{n}$”
只能輸入至少n位數字:“^d{n,}$”
只能輸入m-n位的數字:“^d{m,n}$”
只能輸入零和非零開頭的數字:“^(0|[1-9][0-9]*)$”
只能輸入有兩位小數的正實數:“^[0-9]+(.[0-9]{2})?$”
只能輸入有1-3位小數的正實數:“^[0-9]+(.[0-9]{1,3})?$”
只能輸入非零的正整數:“^+?[1-9][0-9]*$”
只能輸入非零的負整數:“^-[1-9][0-9]*$”
只能輸入長度為3的字元:“^.{3}$”
只能輸入由26個英文字母組成的字串:“^[A-Za-z]+$”
只能輸入由26個大寫英文字母組成的字串:“^[A-Z]+$”
只能輸入由26個小寫英文字母組成的字串:“^[a-z]+$”
只能輸入由數字和26個英文字母組成的字串:“^[A-Za-z0-9]+$”
只能輸入由數字、26個英文字母或者下劃線組成的字串:“^w+$”
驗證使用者密碼:“^[a-zA-Z]w{5,17}$”正確格式為:以字母開頭,長度在6-18之間,
只能包含字元、數字和下劃線。
驗證是否含有^%&',;=?$"等字元:“[^%&',;=?$x22]+”
只能輸入漢字:“^[u4e00-u9fa5],{0,}$”
驗證Email地址:“^w+[-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$”
驗證InternetURL:“^
驗證電話號碼:“^((d{3,4})|d{3,4}-)?d{7,8}$”
正確格式為:“XXXX-XXXXXXX”,“XXXX-XXXXXXXX”,“XXX-XXXXXXX”,
“XXX-XXXXXXXX”,“XXXXXXX”,“XXXXXXXX”。
驗證身份證號(15位或18位數字):“^d{15}|d{}18$”
驗證一年的12個月:“^(0?[1-9]|1[0-2])$”正確格式為:“01”-“09”和“1”“12”
驗證一個月的31天:“^((0?[1-9])|((1|2)[0-9])|30|31)$”
正確格式為:“01”“09”和“1”“31”。
匹配中文字元的正則表示式: [u4e00-u9fa5]
匹配雙位元組字元(包括漢字在內):[^x00-xff]
匹配空行的正則表示式:n[s| ]*r
匹配HTML標記的正則表示式:/<(.*)>.*|<(.*) />/
匹配首尾空格的正則表示式:(^s*)|(s*$)
匹配Email地址的正則表示式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
匹配網址URL的正則表示式:[url=
基本使用的例子(更多資訊參看 官方文件 )
1.
- NSString *searchString = @ "This is neat." ;
- NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)" ;
- NSRange matchedRange = NSMakeRange(NSNotFound, 0UL);
- NSError *error = NULL;
- matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];
- NSLog(@"matchedRange: %@" , NSStringFromRange(matchedRange));
- // 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘
- NSString *matchedString = [searchString substringWithRange:matchedRange];
- NSLog(@"matchedString: '%@'" , matchedString);
- // 2008-03-18 03:51:16.532 test[51583:813] matchedString: 'is' //生成子字串
- NSString *searchString = @"This is neat.";
- NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)";
- NSRange matchedRange = NSMakeRange(NSNotFound, 0UL);
- NSError *error = NULL;
- matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];
- NSLog(@"matchedRange: %@", NSStringFromRange(matchedRange));
- // 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘
- NSString *matchedString = [searchString substringWithRange:matchedRange];
- NSLog(@"matchedString: '%@'", matchedString);
- // 2008-03-18 03:51:16.532 test[51583:813] matchedString: 'is' //生成子字串
2.找到第一個匹配並返回一個NSString
- NSString *searchString = @ "This is neat." ;
- NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)" ;
- NSString *matchedString = [searchString stringByMatching:regexString capture:2L];
- NSLog(@"matchedString: '%@'" , matchedString);
- // 2008-03-18 03:53:42.949 test[51583:813] matchedString: 'is'
- NSString *searchString = @"This is neat.";
- NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)";
- NSString *matchedString = [searchString stringByMatching:regexString capture:2L];
- NSLog(@"matchedString: '%@'", matchedString);
- // 2008-03-18 03:53:42.949 test[51583:813] matchedString: 'is'
3.查詢和替換,加括號和概念和Python中的一樣,$1指代第一個括號中的內容
- NSString *searchString = @ "This is neat." ;
- NSString *regexString = @"//b(//w+)//b" ;
- NSString *replaceWithString = @"{$1}" ;
- NSString *replacedString = NULL;
- replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
- //NSMutableString可以直接替換,並返回替換的次數
- NSLog(@"replaced string: '%@'" , replacedString);
- // 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'
- NSMutableString *mutableString = [NSMutableString stringWithString:@"This is neat." ];
- NSString *regexString = @"//b(//w+)//b" ;
- NSString *replaceWithString = @"{$1}" ;
- NSUInteger replacedCount = 0UL;
- replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];
- NSLog(@"count: %lu string: '%@'" , (u_long)replacedCount, mutableString);
- // 2008-07-01 21:25:43.433 test[69689:813] count: 3 string: '{This} {is} {neat}.'
- NSString *searchString = @"This is neat.";
- NSString *regexString = @"//b(//w+)//b";
- NSString *replaceWithString = @"{$1}";
- NSString *replacedString = NULL;
- replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
- //NSMutableString可以直接替換,並返回替換的次數
- NSLog(@"replaced string: '%@'", replacedString);
- // 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'
- NSMutableString *mutableString = [NSMutableString stringWithString:@"This is neat."];
- NSString *regexString = @"//b(//w+)//b";
- NSString *replaceWithString = @"{$1}";
- NSUInteger replacedCount = 0UL;
- replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];
- NSLog(@"count: %lu string: '%@'", (u_long)replacedCount, mutableString);
- // 2008-07-01 21:25:43.433 test[69689:813] count: 3 string: '{This} {is} {neat}.'
4.用於拆分,返回一個拆分後的字串陣列
- NSString *searchString = @ "This is neat." ;
- NSString *regexString = @"//s+" ;
- NSArray *splitArray = NULL;
- splitArray = [searchString componentsSeparatedByRegex:regexString];
- // splitArray == { @"This", @"is", @"neat." }
- NSLog(@"splitArray: %@" , splitArray);
- NSString *searchString = @"This is neat.";
- NSString *regexString = @"//s+";
- NSArray *splitArray = NULL;
- splitArray = [searchString componentsSeparatedByRegex:regexString];
- // splitArray == { @"This", @"is", @"neat." }
- NSLog(@"splitArray: %@", splitArray);
5.返回所有匹配的字串陣列,這個例子中雖然有多個括號,但是componentsMatchedByRegex不管
- NSString *searchString = @ "$10.23, $1024.42, $3099" ;
- NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))" ;
- NSArray *matchArray = NULL;
- matchArray = [searchString componentsMatchedByRegex:regexString];
- // matchArray == { @"$10.23", @"$1024.42", @"$3099" };
- NSLog(@"matchArray: %@" , matchArray);
- 6.返回所有匹配的字串陣列處理所有的括號
- NSString *searchString = @"$10.23, $1024.42, $3099" ;
- NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))" ;
- NSArray *capturesArray = NULL;
- capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];
- /* capturesArray ==
- [NSArray arrayWithObjects:
- [NSArray arrayWithObjects: @"$10.23", @"10.23", @"10", @"23", NULL],
- [NSArray arrayWithObjects:@"$1024.42", @"1024.42", @"1024", @"42", NULL],
- [NSArray arrayWithObjects: @"$3099", @"3099", @"3099", @"", NULL],
- NULL];
- */
- NSLog(@"capturesArray: %@" , capturesArray);
- 輸出結果:
- shell% ./capturesArray↵
- 2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (
- (
- "$10.23" ,
- "10.23" ,
- 10,
- 23
- ),
- (
- "$1024.42" ,
- "1024.42" ,
- 1024,
- 42
- ),
- (
- "$3099" ,
- 3099,
- 3099,
- ""
- )
-
)
- NSString *searchString = @"$10.23, $1024.42, $3099";
- NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))";
- NSArray *matchArray = NULL;
- matchArray = [searchString componentsMatchedByRegex:regexString];
- // matchArray == { @"$10.23", @"$1024.42", @"$3099" };
- NSLog(@"matchArray: %@", matchArray);
- 6.返回所有匹配的字串陣列處理所有的括號
- NSString *searchString = @"$10.23, $1024.42, $3099";
- NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))";
- NSArray *capturesArray = NULL;
- capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];
- /* capturesArray ==
- [NSArray arrayWithObjects:
- [NSArray arrayWithObjects: @"$10.23", @"10.23", @"10", @"23", NULL],
- [NSArray arrayWithObjects:@"$1024.42", @"1024.42", @"1024", @"42", NULL],
- [NSArray arrayWithObjects: @"$3099", @"3099", @"3099", @"", NULL],
- NULL];
- */
- NSLog(@"capturesArray: %@", capturesArray);
- 輸出結果:
- shell% ./capturesArray↵
- 2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (
- (
- "$10.23",
- "10.23",
- 10,
- 23
- ),
- (
- "$1024.42",
- "1024.42",
- 1024,
- 42
- ),
- (
- "$3099",
- 3099,
- 3099,
- ""
- )
- )
相關推薦
iOS 正則表示式驗證
2.工程中新增libicucore.dylib frameworks。 3.現在所有的nsstring物件就可以呼叫RegexKitLite中的方法了。 NSString *email = @”[email protected]”; [email isM
iOS利用正則表示式驗證郵箱格式是否正確
//利用正則表示式驗證 + (BOOL)isAvailableEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+
JavaScript 正則表示式驗證登入例項
程式碼片段: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>qq登入模擬測試</title> </head> <
正則表示式驗證問題
<!DOCTYPE html><html><body><p>使用者名稱正則表示式 ,4到16位(字母,數字,下滑線,減號)</p><p id="demo"></p><form action="" method="get"
js正則表示式驗證字串只包括大小寫字母下劃線和-
背景說明 在之前的開發過程中為了校驗一個欄位只含有大小寫字母,下劃線和-花費了不少力氣才搞定,想著趕快寫一篇部落格來記錄下來,日後開發一定會遇到!! 程式碼實現 首先定義一個變數用來存放驗證字串的正則表示式:var regex=/^[A-Za-z0-9_\-]+$/ig;
正則表示式驗證url、時間、ip
//驗證url function IsURL(str_url){ var strRegex = "^((https|http|ft
iOS正則表示式簡單應用
想要了解基本語法的可以進入下面網站自行學習 自學地址Runoob 正則表示式在iOS的簡單應用 // 匹配小寫a開頭的字串 NSString * regex = @"^a.*"; NSPredicate *pred = [NSPredicate predicateWithForm
微信小程式-正則表示式(驗證手機號-身份證-郵箱..)
手機號驗證: if (!(/^1[34578]\d{9}$/.test(e.detail.value.phone))) { wx.showToast({ title: '手機號碼有誤', duration: 2000, icon:'none' }); return false
正則表示式驗證合法電話號碼
中國移動:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178/1705 中國聯通:130/131/132/155/156/185/186/145/175/176/1709
正則表示式---驗證身份證號碼
/** * 正則表示式驗證身份證號碼 * * @param integer $num 所要驗證的身份證號碼 * @return boolean */ public static function isPersonalCard($
【javascript】使用正則表示式驗證
管理系統專案上用到的正則表示式驗證: 一、驗證手機號碼 最新的手機號驗證。 function checkMobilePhone(str){ var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|1
使用正則表示式驗證輸入數字
實現效果: 知識運用: 實現程式碼: private void button1_Click(object sender, EventArgs e) { if (IsValidate(textBox1.Text.ToStri
使用正則表示式驗證一個月的31天
實現效果: 關鍵運用: 實現程式碼: public bool IsValidateDay(string str_day) { return System.Text.RegularExpressions.
使用正則表示式驗證月份
實現效果: 運用知識: 實現程式碼: //定義方法 public bool IsValidate(string str_month) { return System.Text.RegularExpressions.
使用正則表示式驗證兩位小數
實現效果: 知識運用: 程式碼實現: private void button1_Click(object sender, EventArgs e) { if (IsValidate(textBox1.Text.ToSt
使用正則表示式驗證身份證號
實現效果: 關鍵知識: 實現程式碼: private void button1_Click(object sender, EventArgs e) { if (IsValidate(textBox1.Text.ToStri
使用正則表示式驗證密碼長度
實現效果: 知識運用: 實現程式碼: private void button1_Click(object sender, EventArgs e) { if (Validate(textBox1.Text.ToString()
Js 正則表示式驗證
手機號碼驗證 130,131,132,133,134,135,136,137,138,139,147,150,151,152,153,155,156,157,158,159,177,173180,181,182,185,186,187,188,189 function ch
js正則表示式驗證價格
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <me
使用正則表示式驗證字母
實現效果: 知識運用: 實現程式碼: static void Main(string[] args) { Console.Title = "使用正則表示式驗證字母"; Console.W