正则表达式 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正则表达式语言的一些特性:
\{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,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 //只替换子表达式的第一次出现

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值