正規表示式在匹配字串,驗證輸入合法性時經常用到.c++ 11標準庫中已經支援了正規表示式.以下對於幾個常用的驗證進行實現,以體會c++正規表示式的用法.
注意:
在c++中,對於特殊字元,需要使用轉義字元. 因此,匹配數字的\d,需要寫成\d這種格式.
經典的三部曲匹配:
1.先寫pattern. string pattern = ;
2.使用re. regex re(pattern);
3.match. bool rs = regex_match(mobile, re);
4.regex_match在匹配一次後即返回結果,如果期望進行多次匹配,需要用到regex_iterator.std::regex_iteratorrit(result.begin(), result.end(), re);
匹配結果的輸出可以利用cmatch.regex_match(rit->str().c_str(), cm, re);
這裡面cm的內容與正規表示式pattern是對應匹配的. 具體可參考matchpriceinfo方法.
程式執行結果:
; //c++中,\符號需要轉義,因此寫作兩個\\符號
regex re(pattern);
rs = regex_match(email, re);
if (rs)
else
return rs;
}bool commonregexmethod::urlcheck(const string url)
; //在括號內()的特殊字元,可以不用轉義,也可以用轉義.括號外的一定要轉義
regex re(pattern);
rs = regex_match(url, re);
if (rs)
else
return rs;
}bool commonregexmethod::ipv4check(const string ipv4)
)|([1]\\d)|([2][0-4]\\d)|([2][5][0-5]))\\.)\
(([0-9])|([1-9]\\d)|([1]\\d)|([2][0-4]\\d)|([2][5][0-5]))"};
regex re(pattern);
rs = regex_match(ipv4, re);
if (rs)
else
return rs;
}bool commonregexmethod::ipv6check(const string ipv6)
:)[0-9a-fa-f])|\
(([0-9a-fa-f]:):[0-9a-fa-f])|\
(([0-9a-fa-f]:):([0-9a-fa-f]:)?[0-9a-fa-f])|\
(([0-9a-fa-f]:):([0-9a-fa-f]:)[0-9a-fa-f])|\
(([0-9a-fa-f]:):([0-9a-fa-f]:)[0-9a-fa-f])|\
(([0-9a-fa-f]:):([0-9a-fa-f]:)[0-9a-fa-f])|\
(([0-9a-fa-f]:)((\\b((25[0-5])|(1\\d)|(2[0-4]\\d)|(\\d))\b)\.)(\\b((25[0-5])|\
(1\\d)|(2[0-4]\\d)|(\\d))\b))|\
(([0-9a-fa-f]:):((\\b((25[0-5])|(1\\d)|(2[0-4]\\d)|(\\d))\\b)\\.)(\\b((25[0-5])|\
(1\\d)|(2[0-4]\\d)|(\\d))\\b))|\
(::([0-9a-fa-f]:)((\\b((25[0-5])|(1\\d)|(2[0-4]\\d)|(\\d))\\b)\\.)(\\b((25[0-5])|\
(1\\d)|(2[0-4]\\d)|(\\d))\\b))|\
([0-9a-fa-f]::([0-9a-fa-f]:)[0-9a-fa-f])|\
(::([0-9a-fa-f]:)[0-9a-fa-f])|(([0-9a-fa-f]:):))"};
regex re(pattern);
rs = regex_match(ipv6, re);
if (rs)
else
return rs;
}bool commonregexmethod::creditcardcheck(const string creditcard)
)|(5[1-5]\\d)|(6011)|(62\\d))-?\\d-?\\d-?\\d|3[4,7]\\d"};
regex re(pattern);
rs = regex_match(creditcard, re);
if (rs)
else
return rs;
}bool commonregexmethod::passwordcheck(const string password)
;string pattern2 = ;
string pattern3 = ;
string pattern4 = ;
regex re(pattern1);
rs = regex_match(password, re);
if (rs)
else
}else
}else
}else
}else
return rs;
}void commonregexmethod::vaguesearch(const string* searchspace, unsigned int searchspacelen, const string searchstr)
;regex re(pattern);
for (unsigned int i = 0; i < searchspacelen; i++)
searchspace++;
}searchspace = oriptr;
}void commonregexmethod::matchpriceinfo(const char* filename, const string pattern, const unsigned int numinpattern)
++rit;}}
else
delete buf;
buf = null;
return;
}ostringstream* commonregexmethod::readfileintostring(const char* filename)
}return buf;
}bool commonregexmethod::convert(const char c, int &rs)
return result;
}部分測試**如下:
void
testregex()
C 正規表示式 匹配
有時我們需要匹配像 100 50 15 這樣的可巢狀的層次性結構 這時簡單地使用 則只會匹配到最左邊的左括號和最右邊的右括號之間的內容 這裡我們討論的是貪婪模式,懶惰模式也有下面的問題 假如原來的字串裡的左括號和右括號出現的次數不相等,比如 5 3 2 那我們的匹配結果裡兩者的個數也不會相等。有沒有...
常用正規表示式 用於匹配
正規表示式用於字串處理 表單驗證等場合,實用高效。現將一些常用的表示式收集於此,以備不時之需。匹配中文字元的正規表示式 u4e00 u9fa5 評注 匹配中文還真是個頭疼的事,有了這個表示式就好辦了 匹配雙位元組字元 包括漢字在內 x00 xff 評注 可以用來計算字串的長度 乙個雙位元組字元長度計...
正規表示式常用匹配
測試 正規表示式常用匹配qt舉例 qregexp rx rx.setpatternsyntax qregexp regexp rx.setcasesensitivity qt casesensitive 大小寫敏感 rx.setpattern qstring a za z0 9 匹配所有大小寫字母和...