正则表达式 c++

前端之家收集整理的这篇文章主要介绍了正则表达式 c++前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_403_1@ regular expression正则表达式

regex表达有一个正则表达式的类@H_403_1@ regex_match将一个字符序列与一个正则表达式匹配@H_403_1@ regex_search寻找第一个与正则表达式的子序列@H_403_1@ regex_replace使用给定格式替换一个正则表达式@H_403_1@ sregex_iterator迭代器适配器,调用regex_search来遍历一个string中所有匹配的子串@H_403_1@ smatch容器类,保存在string中搜索的结果@H_403_1@ ssub_matchstring中匹配的子表达式的结果

std::string pattern("[^c]ei");//匹配任意不是c的后接ei的字符串@H_403_1@ pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";//匹配单词@H_403_1@ std::regex r(pattern);

std::string ss = "cei";@H_403_1@ r.assign("[[:alpha:]]*" + ss + "[[:alpha:]]*");//赋予新值@H_403_1@ //assign类似下面的=号@H_403_1@ std::string ss = "cei";@H_403_1@ std::regex rs("[[:alpha:]]*" + ss + "[[:alpha:]]*");@H_403_1@ r = rs;

r.mark_cout()//r中子表达式的数目@H_403_1@ r.flags()//返回r的标志集@H_403_1@ ##构造函数和赋值操作可能抛出regex_error的异常@H_403_1@ 标志集:@H_403_1@ icase在匹配过程中忽略大小写@H_403_1@ nosubs不保存匹配的子表达式@H_403_1@ optimize执行速度优先于构造速度@H_403_1@ ECMAScript使用ECMA-262指定的语法@H_403_1@ basic使用POSIX基本的正则表达式语法@H_403_1@ extened 使用POSIX扩展的正则表达式语法@H_403_1@ awk使用POSIX版本的awk语言的语法@H_403_1@ grep使用POSIX版本的grep语法@H_403_1@ egrep使用POSIX版本的egrep语法

regex r("[[:alnum:]] + \\.(cpp|cxx|cc)$",regex::icase);//一个或多个字母或数字字符后接一个'.'再接。。。。@H_403_1@ .和\都是特殊字符

###正则表达式不是由C++编译器解释的,而是在运行时解析的!!!!!!!

try{@H_403_1@ std::regex rtest("[[:alpha:" + ss + "[[:alpha:]]*");@H_403_1@ } catch (regex_error e)@H_403_1@ {@H_403_1@ cout << e.what() << "\ncode:" << e.code() <<endl;@H_403_1@ }

定义在regex和regex_constants::error_type中@H_403_1@ error_collate无效的元素校对请求@H_403_1@ error_ctype无效的字符类

正则表达式是在运行时而非编译时编译的,并且是一个非常慢的操作,尽量创建不必要的regex,循环外使用正则表达式。

regex类保存类型char的正则表达式;@H_403_1@ wregex类保存类型wchar_t的正则表达式。

cmatch表示字符数组序列@H_403_1@ smatch表示string类型的输入序列@H_403_1@ wcmatch..wchar_t@H_403_1@ wsmatch..wstring

匹配关系@H_403_1@ stringregex,smatch,ssub_match,sregex_iterator@H_403_1@ const char*regex,cmatch,csub_match,cregex+iterator@H_403_1@ wstringwregex,wsmatch,wssub_match,wsregex_iterator@H_403_1@ const wchar_t*wregex,wcmatch,wcsub_match,wcregex+iterator

string test_str = "receipt freind theif receive";@H_403_1@ for(sregex_iterator it(test_str.begin(),test_str.end(),r),end_it; it != end_it ; ++it)@H_403_1@ {@H_403_1@ cout << (*it).str() << endl;@H_403_1@ }@H_403_1@ //end_it是一个空sregex_iterator,起到尾后迭代器的作用。

匹配类型的两个名为prefix和suffix的成员,分别返回表示输入序列中当前匹配之前和之后部分的ssub_match对象;@H_403_1@ ssub_match两个成员str和length

smatch m;@H_403_1@ m.ready()//regex_search,regex_match设置过m,retrun true;@H_403_1@ m.size()//匹配失败返回0,否则返回最近一次匹配的正则表达式中子表达式的数目@H_403_1@ m.empty()//m.size()为0,返回true@H_403_1@ m.perfix()@H_403_1@ m.suffix()@H_403_1@ m.format(..)@H_403_1@ m.length(n)//第n个匹配的子表达式的大小@H_403_1@ m.position(n)//第n个子表达式距序列开始的距离@H_403_1@ m.str(n)//第n个子表达式匹配的string@H_403_1@ m[n]//第n个子表达式的ssub_match对象@H_403_1@ m.begin(),m.end()@H_403_1@ m.cbegin(),m.cend()//返回const_iterator

std::regex r("([[:alpha:]]*)\\.(cpp|cc)");@H_403_1@ smatch results;@H_403_1@ string test_str = "receipt freind.cpp theif receive.cc";@H_403_1@ for(sregex_iterator it(test_str.begin(),end_it; it != end_it ; ++it)@H_403_1@ {@H_403_1@ cout << (*it).str(0) << endl;@H_403_1@ cout <<"###-" << it->str(1) << "-###-" << it->str(2) << "-###" << endl;@H_403_1@ }@H_403_1@ //str(0),表示整个模式对应的匹配; str(1)第一个子表达式 。。。。

ECMAScript正则表达式语言的一些特性:@H_403_1@ \{d}表示单个数字@H_403_1@ \{d}{n}表示n个数字的序列@H_403_1@ \{d}{3}匹配3个数字的序列

[] 表示匹配其中字符集中任意一个

string phone = "(\\()?(\\d{3})(\\))?([-. ]?)(\\d{3})([-. ]?)(\\d{4})";@H_403_1@ 其中有7个子表达式, smatch对象会包含8个ssub_match元素,位置[0]的元素表示整个匹配,元素[1]...[7]表示每个对应的子表达式。

ssub_match,wcsub_match成员:@H_403_1@ matched一个public boo数据成员,指出此ssub_match是否匹配了@H_403_1@ first指向匹配序列首元素和尾后位置的迭代器@H_403_1@ second@H_403_1@ length()匹配的大小,如果matched is flase,its size will be zero.@H_403_1@ s = ssub等价 s = ssub.str();

regex_replace

smatch m;@H_403_1@ m.format(dest,FMT,mft)//其中mft是match_flag_type@H_403_1@ m.format(FMT,mft) return string object@H_403_1@ regex_replace(dest,SEQ,r,mft)调用regex_search查找与regex对象r匹配的子串,按FMT格式输出到dest.@H_403_1@ regex_replace(SEQ,mft)

string phone = "(\\()?(\\d{3})(\\))?([-. ]?)(\\d{3})([-. ]?)(\\d{4})";@H_403_1@ regex r(phone);@H_403_1@ string fmt = "$2.$5.$7";//is same to perl@H_403_1@ string number = "(111) 222-3333";@H_403_1@ cout << regex_replace(number,fmt) << endl;//111.222.3333

while(getline(cin,number))@H_403_1@ {@H_403_1@ cout << regex_replace(number,fmt) << endl;@H_403_1@ }

cout << regex_replace(number,fmt) << endl;//输入111 333 4444 aaa输出111.333.4444 aaa@H_403_1@ cout << regex_replace(number,fmt,regex_constants::format_no_copy) << endl;//输入111 333 4444 aaa输出111.333.4444

match_flag_type 定义在regx_constants命名空间,regex_constants定义在std中@H_403_1@ using namespace std::regex_constants;@H_403_1@ 或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//只替换子表达式的第一次出现

猜你在找的正则表达式相关文章