regular expression正则表达式
regex表达有一个正则表达式的类 regex_match将一个字符序列与一个正则表达式匹配 regex_search寻找第一个与正则表达式的子序列 regex_replace使用给定格式替换一个正则表达式 sregex_iterator迭代器适配器,调用regex_search来遍历一个string中所有匹配的子串 smatch容器类,保存在string中搜索的结果 ssub_matchstring中匹配的子表达式的结果
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
匹配关系 stringregex,smatch,ssub_match,sregex_iterator const char*regex,cmatch,csub_match,cregex+iterator wstringwregex,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(),end_it; it != end_it ; ++it) { cout << (*it).str(0) << endl; cout <<"###-" << it->str(1) << "-###-" << it->str(2) << "-###" << endl; } //str(0),表示整个模式对应的匹配; str(1)第一个子表达式 。。。。
ECMAScript正则表达式语言的一些特性: {d}表示单个数字 {d}{n}表示n个数字的序列 {d}{3}匹配3个数字的序列
[] 表示匹配其中字符集中任意一个
string phone = "(()?(d{3})())?([-. ]?)(d{3})([-. ]?)(d{4})"; 其中有7个子表达式, smatch对象会包含8个ssub_match元素,位置[0]的元素表示整个匹配,元素[1]...[7]表示每个对应的子表达式。
ssub_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,mft)调用regex_search查找与regex对象r匹配的子串,按FMT格式输出到dest. regex_replace(SEQ,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,fmt) << endl;//111.222.3333
while(getline(cin,number)) { cout << regex_replace(number,fmt) << endl; }
cout << regex_replace(number,fmt) << endl;//输入111 333 4444 aaa输出111.333.4444 aaa 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中 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//只替换子表达式的第一次出现 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|