Chapter 18.正則表達式庫regex

正則表達式

關於正則表達式就不多做介紹了,vs2010中就已經支持這個c++的新特性,使用時可加入頭文件regex,同時其被加入了std命名空間

#include <regex>

下面就簡單的介紹regex類的三個成員函數,同時對於regex迭代器的用法就不多說了,用到時再總結添加

1.regex_match
嘗試匹配正則表達式的整個字符序列
/*
iter BidirectionalIterator
match_results match_results<const iter/const char*/string,Alloc>& match_results
regex const basic_regex<CharT,Traits>& regex
flags match_flag_type flags = regex_constants::match_default
str const char*
string basic_string
*/
1.bool regex_match(iter,iter,regex,flags)
2.bool regex_match(iter,iter,match_results,regex,flags)
3.bool regex_match(str,regex,flags)
4.bool regex_match(str,match_results,regex,flags)
5.bool regex_match(string,regex,flags)
6.bool regex_match(string,match_results,regex,flags)


If the match does not exist:
m.ready() == true//m is match_results
m.empty() == true
m.size() == 0
If the match exists:
m.ready() true
m.empty() false
m.size() number of subexpressions plus 1, that is, 1+e.mark_count()

eg:
	regex r("^select ([a-zA-Z]*) from ([a-zA-Z]*)$");
	cmatch what;
	bool ret=regex_match("select username from admin",what,r);//4 version
	cout<<ret<<endl;
	if (ret)
	{	
		for (size_t i = 0; i != what.size(); ++i)
		{
			cout<<what[i+1].first<<'\t';
		}
	}
Output:

username admin

eg://這個是以前regex中boost中時寫的,在vs2010下基本也應該相同
	try{
				//^	行開頭
				//()	捕獲組
				//[|]	[]字符類 |或
				//{}	量詞 恰好幾次
				//$	行結尾
		std::string phone("^(1[3|4|5|8|9][0-9]{9})|(15[89][0-9]{8})$");//構造手機號碼正則表達式
		regex reg(phone);
		std::string str_target("13975311105");
		bool r = boost::regex_match(str_target,reg);
		std::cout << " r = " << r << std::endl;
	}
	catch(boost::regex_error &e){
		std::cerr << e.what() << std::endl;
	}

2.regex_search
嘗試匹配正則表達式的字符序列的任何部分
/*
iter BidirectionalIterator
match_results match_results<const iter/const char*/string,Alloc>& match_results
regex const basic_regex<CharT,Traits>& regex
flags match_flag_type flags = regex_constants::match_default
str const char*
string basic_string
*/
1.bool regex_search(iter,iter,regex,flags)
2.bool regex_search(iter,iter,match_results,regex,flags)
3.bool regex_search(str,regex,flags)
4.bool regex_search(str,match_results,regex,flags)
5.bool regex_search(string,regex,flags)
6.bool regex_search(string,match_results,regex,flags)

eg://這個是以前regex中boost中時寫的,在vs2010下基本也應該相同
        boost::cmatch mat;
        boost::regex reg( "\\d+" );    //查找字符串裏的數字
        if(boost::regex_search(szStr, mat, reg))
        {
            cout << "searched:" << mat[0] << endl;
        }

3.regex_replace
以格式化的替換文本來替換正則表達式匹配的地方
/*
outIter OutputIterator
iter BidirectionalIterator
match_results match_results<const iter/const char*/string,Alloc>& match_results
regex const basic_regex<CharT,Traits>& regex
flags match_flag_type flags = regex_constants::match_default
fmtStr const char*
fmtString basic_string
*/
關於fmtStr/fmtString
$& 表示替換所有匹配的子表達式
$N $N表示匹配的子表達式, N 爲子表達式索引
eg:
$1$3 表示替換第1個和第3個匹配的子表達式,去掉第2個

1.outIter regex_replace(outIter,iter,iter,regex,fmtStr,flags)
2.outIter regex_replace(outIter,iter,iter,regex,fmtString,flags)
3.string regex_replace(str,regex,fmtStr,flags)
4.string regex_replace(str,regex,fmtString,flags)
5.string regex_replace(string,regex,fmtStr,flags)
6.string regex_replace(string,regex,fmtString,flags)


eg:
	string text="Quick brown fox";
	regex vowel_re("a|o|e|u|i");
	cout<<regex_replace(text,vowel_re,string("[$&]"))<<endl;//6 version
	
	Output:
	Q[u][i]ck br[o]wn f[o]x
	
eg:
	regex reg("(Colo)(u)(r)", regex::icase|regex::ECMAScript);
	string s="Colour, colours, color, colourize";
	s=regex_replace(s,reg,string("$1$3"));//替換第1個 Colo 和第3個 r,去掉 u
	//s=regex_replace(s,reg,string("$1"));//替換第1個 Colo,去掉第2個和第3個u r
	cout<<s;
	Output:
	Color, colors, color, colorize
	//Colo, colos, color, coloize
發佈了39 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章