正则表达式 c++
regular expression?正則表達式
regex??表達有一個正則表達式的類
regex_match?將一個字符序列與一個正則表達式匹配
regex_search?尋找第一個與正則表達式的子序列
regex_replace?使用給定格式替換一個正則表達式
sregex_iterator?迭代器適配器,調用regex_search來遍歷一個string中所有匹配的子串
smatch?容器類,保存在string中搜索的結果
ssub_match?string中匹配的子表達式的結果
std::string pattern("[^c]ei");?//匹配任意不是c的后接ei的字符串
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";?//匹配單詞
std::regex r(pattern);
std::string ss = "cei";
r.assign("[[:alpha:]]*" + ss + "[[:alpha:]]*");??//賦予新值
//assign類似下面的=號
std::string ss = "cei";
std::regex rs("[[:alpha:]]*" + ss + "[[:alpha:]]*");
r = rs;
r.mark_cout()??//r中子表達式的數目
r.flags()????//返回r的標志集
##構造函數和賦值操作可能拋出regex_error的異常
標志集:
icase?在匹配過程中忽略大小寫
nosubs?不保存匹配的子表達式
optimize?執行速度優先于構造速度
ECMAScript?使用ECMA-262指定的語法
basic??使用POSIX基本的正則表達式語法
extened 使用POSIX擴展的正則表達式語法
awk?使用POSIX版本的awk語言的語法
grep?使用POSIX版本的grep語法
egrep?使用POSIX版本的egrep語法
regex r("[[:alnum:]] + \\.(cpp|cxx|cc)$",regex::icase);?//一個或多個字母或數字字符后接一個'.'再接。。。。
.和\都是特殊字符
###正則表達式不是由C++編譯器解釋的,而是在運行時解析的!!!!!!!
try{
?std::regex rtest("[[:alpha:" + ss + "[[:alpha:]]*");
} catch (regex_error e)
{
?cout << e.what() << "\ncode:" << e.code() <<endl;
}
定義在regex和regex_constants::error_type中
error_collate?無效的元素校對請求
error_ctype?無效的字符類
正則表達式是在運行時而非編譯時編譯的,并且是一個非常慢的操作,盡量創建不必要的regex,循環外使用正則表達式。
regex類保存類型char的正則表達式;
wregex類保存類型wchar_t的正則表達式。
cmatch?表示字符數組序列
smatch?表示string類型的輸入序列
wcmatch?..wchar_t
wsmatch?..wstring
匹配關系
string?????regex,smatch,ssub_match,sregex_iterator
const char*???regex,cmatch,csub_match,cregex+iterator
wstring?????wregex,wsmatch,wssub_match,wsregex_iterator
const wchar_t*?wregex,wcmatch,wcsub_match,wcregex+iterator
string test_str = "receipt freind theif receive";
for(sregex_iterator it(test_str.begin(),test_str.end(),r),end_it; it != end_it ; ++it)
{
??cout << (*it).str() << endl;
}
//end_it是一個空sregex_iterator,起到尾后迭代器的作用。
匹配類型的兩個名為prefix和suffix的成員,分別返回表示輸入序列中當前匹配之前和之后部分的ssub_match對象;
ssub_match兩個成員str和length
smatch m;
m.ready()???//regex_search , regex_match設置過m, retrun true;
m.size()??//匹配失敗返回0,否則返回最近一次匹配的正則表達式中子表達式的數目
m.empty()??//m.size()為0,返回true
m.perfix()
m.suffix()
m.format(..)
m.length(n)??//第n個匹配的子表達式的大小
m.position(n)?//第n個子表達式距序列開始的距離
m.str(n)???//第n個子表達式匹配的string
m[n]?????//第n個子表達式的ssub_match對象
m.begin(),m.end()
m.cbegin(),m.cend()?//返回const_iterator
std::regex r("([[:alpha:]]*)\\.(cpp|cc)");
smatch results;
string test_str = "receipt freind.cpp theif receive.cc";
for(sregex_iterator it(test_str.begin(),test_str.end(),r),end_it; it != end_it ; ++it)
{
?cout << (*it).str(0) << endl;
?cout <<"###-" << it->str(1) << "-###-" << it->str(2) << "-###" << endl;
}
//str(0),表示整個模式對應的匹配; str(1)第一個子表達式 。。。。
ECMAScript正則表達式語言的一些特性:
\ze8trgl8bvbq?表示單個數字
\ze8trgl8bvbq{n}?表示n個數字的序列
\ze8trgl8bvbq{3}?匹配3個數字的序列
[] 表示匹配其中字符集中任意一個
string phone = "(\\()?(\\d{3})(\\))?([-. ]?)(\\d{3})([-. ]?)(\\d{4})";
其中有7個子表達式, smatch對象會包含8個ssub_match元素,位置[0]的元素表示整個匹配,元素[1]...[7]表示每個對應的子表達式。
ssub_match,csub_match,wssub_match,wcsub_match成員:
matched??一個public boo數據成員,指出此ssub_match是否匹配了
first???指向匹配序列首元素和尾后位置的迭代器
second
length()?匹配的大小,如果matched is flase, its size will be zero.
s = ssub?等價 s = ssub.str();
regex_replace
smatch m;
m.format(dest,FMT,mft)?//其中mft是match_flag_type
m.format(FMT,mft) ?return string object
regex_replace(dest,SEQ,r,FMT,mft)?調用regex_search查找與regex對象r匹配的子串,按FMT格式輸出到dest.
regex_replace(SEQ,r,FMT,mft)
string phone = "(\\()?(\\d{3})(\\))?([-. ]?)(\\d{3})([-. ]?)(\\d{4})";
regex r(phone);
string fmt = "$2.$5.$7";???//is same to perl
string number = "(111) 222-3333";
cout << regex_replace(number,r,fmt) << endl;??//111.222.3333
while(getline(cin,number))
{
?cout << regex_replace(number,r,fmt) << endl;
}
?cout << regex_replace(number,r,fmt) << endl;?//輸入111 333 4444 aaa???輸出111.333.4444 aaa
?cout << regex_replace(number,r,fmt,regex_constants::format_no_copy) << endl;//輸入111 333 4444 aaa???輸出111.333.4444
match_flag_type 定義在regx_constants命名空間,regex_constants定義在std中
using namespace std::regex_constants;
或using std::regex_constants::format_no_copy
match_flag_type成員:
match_default???//same to?? format_default
match_not_bol???//不將首字符作為行首處理
match_not_eol???//不將尾字符作為行尾處理
match_not_bow???//不將首字符作為單詞首處理
match_not_eow???//不將尾字符作為單詞尾處理
match_any?????//如果多于一個匹配,則可返回任意一個
match_not_null??//不匹配任意空序列
match_continuous?//匹配必須從輸入的首字符開始
match_prev_avail?//輸入序列包含第一個匹配之前的內容
format_default??//用ECMAScript規則替換
format_sed????//用POSI sed規則替換
format_no_copy??//不輸出輸入序列中未匹配的部分
format_first_only?//只替換子表達式的第一次出現
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
- 上一篇: 正则表达式 perl
- 下一篇: DLL 延时加载