C++正则表达式的使用
生活随笔
收集整理的這篇文章主要介紹了
C++正则表达式的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C++里面使用正則表達式一般有三種:C regex,C ++regex,boost regex
C regex 的速度是最快的
C++ regex 速度一般
boost regex 速度最慢,但是用起來最方便
速度上大約是這么個情況:V(C)=5V(C++)=10(Boost)
[聲明:以上速度是個人測試,僅供參考]
下面看用法:
C++版本:
// Regex.cpp : 定義控制臺應用程序的入口點。 // #include <regex> #include <iostream> #include <stdio.h> #include <string> using namespace std;//電子郵件匹配 bool is_email_valid(const std::string& email) {const regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");/**** const std::regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+"); std:: match_results<std::string::const_iterator> result; bool valid = std::regex_match(email, result,pattern);//此處result參數可有可無,result是一個字符串數組,用來存儲正則表達式里面括號的內容。if(valid&&(result.length()>0)){for(int i =0;i<result.length();i++){cout<<result[i]<<endl;}}return valid; }//IP地址匹配bool is_IPAddress_valid(const std::string& ipaddress) { const std::regex pattern("(\\d{1,3}).(\\d{1,3}).(\\d{1,3}).(\\d{1,3})");//三位數的都可以,沒有設置1-255 的條件 std:: match_results<std::string::const_iterator> result; bool valid = std::regex_match(ipaddress, result, pattern);if(valid&&(result.length()>0)){for(int i =0;i<result.length();i++){cout<<result[i]<<endl;}}return valid; }int main(int argc,char** argv) {cout<<"測試4個電子郵件選項"<<endl; std::string email1 = "marius.bancila@domain.com"; std::string email2 = "mariusbancila@domain.com"; std::string email3 = "marius_b@domain.co.uk"; std::string email4 = "marius@domain"; std::cout << email1 << " : " << (is_email_valid(email1) ? "valid" : "invalid") << std::endl; std::cout << email2 << " : " << (is_email_valid(email2) ? "valid" : "invalid") << std::endl; std::cout << email3 << " : " << (is_email_valid(email3) ? "valid" : "invalid") << std::endl; std::cout << email4 << " : " << (is_email_valid(email4) ? "valid" : "invalid") << std::endl;cout<<"測試4個IP地址選項"<<endl; std::string IPAddress1 = "202.20.144.3"; std::string IPAddress2 = "255.02.233.02"; std::string IPAddress3 = "127.256.2.36"; std::string IPAddress4 = "123.-25.56.125"; std::cout << IPAddress1 << " : " << (is_IPAddress_valid(IPAddress1) ? "valid" : "invalid") << std::endl; std::cout << IPAddress2 << " : " << (is_IPAddress_valid(IPAddress2) ? "valid" : "invalid") << std::endl; std::cout << IPAddress3 << " : " << (is_IPAddress_valid(IPAddress3) ? "valid" : "invalid") << std::endl; std::cout << IPAddress4 << " : " << (is_IPAddress_valid(IPAddress4) ? "valid" : "invalid") << std::endl;return 0 ; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
const std::regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");首先注意‘()’表示將正則表達式分成子表達式,每個‘()’之間的內容表示一個子表達式;‘\’是一個轉義字符,‘\’表示扔掉第二個‘\’的轉義特性,‘\w+’表示匹配一個或多個單詞,‘+’表示重復一次或者多次,因此第一個子表達式的意思就是匹配一個或者多個單詞;接著看第二個子表達式,‘|’表示選擇,出現‘.’或者‘’,后面的‘?’表示該子表示出現一次或者零次,因此第二個子表示表示‘.’或‘’出現不出現都匹配。第三個子表達式表示出現一個單詞,‘*’表示任意個字符。
總結
以上是生活随笔為你收集整理的C++正则表达式的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初学C++正则表达式(regex)
- 下一篇: 在C/C++语言中使用正则表达式