正则表达式 c++
regular expression?正則表達(dá)式
regex??表達(dá)有一個(gè)正則表達(dá)式的類
regex_match?將一個(gè)字符序列與一個(gè)正則表達(dá)式匹配
regex_search?尋找第一個(gè)與正則表達(dá)式的子序列
regex_replace?使用給定格式替換一個(gè)正則表達(dá)式
sregex_iterator?迭代器適配器,調(diào)用regex_search來(lái)遍歷一個(gè)string中所有匹配的子串
smatch?容器類,保存在string中搜索的結(jié)果
ssub_match?string中匹配的子表達(dá)式的結(jié)果
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類似下面的=號(hào)
std::string ss = "cei";
std::regex rs("[[:alpha:]]*" + ss + "[[:alpha:]]*");
r = rs;
r.mark_cout()??//r中子表達(dá)式的數(shù)目
r.flags()????//返回r的標(biāo)志集
##構(gòu)造函數(shù)和賦值操作可能拋出regex_error的異常
標(biāo)志集:
icase?在匹配過(guò)程中忽略大小寫
nosubs?不保存匹配的子表達(dá)式
optimize?執(zhí)行速度優(yōu)先于構(gòu)造速度
ECMAScript?使用ECMA-262指定的語(yǔ)法
basic??使用POSIX基本的正則表達(dá)式語(yǔ)法
extened 使用POSIX擴(kuò)展的正則表達(dá)式語(yǔ)法
awk?使用POSIX版本的awk語(yǔ)言的語(yǔ)法
grep?使用POSIX版本的grep語(yǔ)法
egrep?使用POSIX版本的egrep語(yǔ)法
regex r("[[:alnum:]] + \\.(cpp|cxx|cc)$",regex::icase);?//一個(gè)或多個(gè)字母或數(shù)字字符后接一個(gè)'.'再接。。。。
.和\都是特殊字符
###正則表達(dá)式不是由C++編譯器解釋的,而是在運(yùn)行時(shí)解析的!!!!!!!
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?無(wú)效的元素校對(duì)請(qǐng)求
error_ctype?無(wú)效的字符類
正則表達(dá)式是在運(yùn)行時(shí)而非編譯時(shí)編譯的,并且是一個(gè)非常慢的操作,盡量創(chuàng)建不必要的regex,循環(huán)外使用正則表達(dá)式。
regex類保存類型char的正則表達(dá)式;
wregex類保存類型wchar_t的正則表達(dá)式。
cmatch?表示字符數(shù)組序列
smatch?表示string類型的輸入序列
wcmatch?..wchar_t
wsmatch?..wstring
匹配關(guān)系
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是一個(gè)空sregex_iterator,起到尾后迭代器的作用。
匹配類型的兩個(gè)名為prefix和suffix的成員,分別返回表示輸入序列中當(dāng)前匹配之前和之后部分的ssub_match對(duì)象;
ssub_match兩個(gè)成員str和length
smatch m;
m.ready()???//regex_search , regex_match設(shè)置過(guò)m, retrun true;
m.size()??//匹配失敗返回0,否則返回最近一次匹配的正則表達(dá)式中子表達(dá)式的數(shù)目
m.empty()??//m.size()為0,返回true
m.perfix()
m.suffix()
m.format(..)
m.length(n)??//第n個(gè)匹配的子表達(dá)式的大小
m.position(n)?//第n個(gè)子表達(dá)式距序列開始的距離
m.str(n)???//第n個(gè)子表達(dá)式匹配的string
m[n]?????//第n個(gè)子表達(dá)式的ssub_match對(duì)象
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),表示整個(gè)模式對(duì)應(yīng)的匹配; str(1)第一個(gè)子表達(dá)式 。。。。
ECMAScript正則表達(dá)式語(yǔ)言的一些特性:
\ozvdkddzhkzd?表示單個(gè)數(shù)字
\ozvdkddzhkzd{n}?表示n個(gè)數(shù)字的序列
\ozvdkddzhkzd{3}?匹配3個(gè)數(shù)字的序列
[] 表示匹配其中字符集中任意一個(gè)
string phone = "(\\()?(\\d{3})(\\))?([-. ]?)(\\d{3})([-. ]?)(\\d{4})";
其中有7個(gè)子表達(dá)式, smatch對(duì)象會(huì)包含8個(gè)ssub_match元素,位置[0]的元素表示整個(gè)匹配,元素[1]...[7]表示每個(gè)對(duì)應(yīng)的子表達(dá)式。
ssub_match,csub_match,wssub_match,wcsub_match成員:
matched??一個(gè)public boo數(shù)據(jù)成員,指出此ssub_match是否匹配了
first???指向匹配序列首元素和尾后位置的迭代器
second
length()?匹配的大小,如果matched is flase, its size will be zero.
s = ssub?等價(jià) 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)?調(diào)用regex_search查找與regex對(duì)象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?????//如果多于一個(gè)匹配,則可返回任意一個(gè)
match_not_null??//不匹配任意空序列
match_continuous?//匹配必須從輸入的首字符開始
match_prev_avail?//輸入序列包含第一個(gè)匹配之前的內(nèi)容
format_default??//用ECMAScript規(guī)則替換
format_sed????//用POSI sed規(guī)則替換
format_no_copy??//不輸出輸入序列中未匹配的部分
format_first_only?//只替換子表達(dá)式的第一次出現(xiàn)
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結(jié)
- 上一篇: 正则表达式 perl
- 下一篇: 注入(二):修改导入表(c++)