regex类表示一个正则表达式,regex_match和regex_search确定一个给定字符串与一个给定的regex是否匹配,如果整个字符串与regex匹配则regex_match返回true,如果字符串中的一个子串与regex匹配,则regex_search返回true。如果它们的参数中包含smatch参数,成功匹配的相关信息存在该参数中。regex_replace将给定的字符串替换为另外一种形式。
int main()
{
regex r1("[0-9]+");
smatch results1;
cout << boolalpha << regex_match("123", r1) << endl;//true
cout << regex_match("12a", r1) << endl;//false
string str1{ "abc12345de111" };
if (regex_search(str1, results1, r1)) {
cout << results1.str()<< endl;//123456
}
regex r2("[0-9]+[a-z]+");
cout << boolalpha << regex_match("123", r2) << endl;//false
cout << regex_match("12a", r2) << endl;//true
system("pause");
}
指定regex对象的选项
当我们定义一个regex或对一个regex调用assign为其赋新值时,可以指定一些标志来影响regex如何操作,默认值为ECMAScript ,从而使regex会使用ECMA-262规范,这是很多Web浏览器使用的正则表达式语言。
static constexpr flag_type icase = regex_constants::icase;//匹配过程中忽略大小写
static constexpr flag_type nosubs = regex_constants::nosubs;//不保存匹配的子表达式
static constexpr flag_type optimize = regex_constants::optimize;//执行速度优先于构造速度
static constexpr flag_type collate = regex_constants::collate;//形如 "[a-b]" 的字符范围将对本地环境敏感
static constexpr flag_type ECMAScript = regex_constants::ECMAScript;//使用ECMA-262指定的语法
static constexpr flag_type basic = regex_constants::basic;//使用POSIX基本的正则表达式语法
static constexpr flag_type extended = regex_constants::extended;//使用POSIX扩展的正则表达式语法
static constexpr flag_type awk = regex_constants::awk;//使用POSIX版本的awk语言的语法
static constexpr flag_type grep = regex_constants::grep;//使用POSIX版本的grep的语法
static constexpr flag_type egrep = regex_constants::egrep;//使用POSIX版本的egrep的语法
int main()
{
regex r1("[a-z]+");//忽略大小写
cout << boolalpha << regex_match("abc", r1) << endl;//true
cout << regex_match("ABC", r1) << endl;//false
regex r2("[a-z]+",regex::icase|regex::ECMAScript);//忽略大小写
cout << boolalpha << regex_match("abc", r2) << endl;//true
cout << regex_match("ABC", r2) << endl;//true
system("pause");
}
正则表达式异常处理
如果编写的正则表达式存在错误,则在运行时标准库会抛出一个类型为regex_error的异常。
int main()
{
try {
regex r1("[a-z");
}catch (regex_error e) {
//regex_error(error_brack) : The expression contained mismatched[and].
//code : 4
cout <<e.what() <<endl<<"code:"<< e.code() << endl;
}
system("pause");
}
正则表达式类型和字符串类型
字符串类型 | 正则表达式类型 |
---|---|
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 |
regex迭代器
使用sregex_iterator可以获得所有匹配结果,将sregex_iterator绑定到一个字符串和一个regex对象时,sregex_iterator会调用regex_search查找第一个匹配位置,解引用迭代器时会得到最近一次搜索结果的smatch对象,当我们递增迭代器时,它调用regex_search查找下一个匹配。
sregex_iterator有两个名为prefix和suffix的成员,分别返回当前匹配之前和之后部分的ssub_match对象。
int main()
{
string pattern("[0-9]+");
regex r1(pattern);
string str = "12234abc11111def2222ghj3333";
for (sregex_iterator it(str.begin(), str.end(), r1), endIt; it != endIt; ++it) {
cout <<it->prefix().str()<<" "<< it->str() <<" "<<it->suffix().str()<< endl;
}
//12234 abc11111def2222ghj3333
// abc 11111 def2222ghj3333
// def 2222 ghj3333
// ghj 3333
system("pause");
}
子表达式匹配
正则表达式中通常包含一个或多个子表达式,子表达式通常用括号表示,在使用smatch保存匹配结果时,str(),str(0)表示整体正则表达式的匹配,str(1)表示第一个子表达式的匹配,str(2)表示第二个子表达式的匹配,依此类推。
smatch包含多个ssub_match元素,位置0的元素表示整体匹配结果,其余元素表示子表达式的匹配结果,如果匹配了则matched成员为true,如果整体都没匹配,则子表达式均不匹配。
int main()
{
string pattern("([0-9]+)([a-z]+)");
regex r(pattern);
string str1 = "12234abc";
smatch result1;
if (regex_search(str1, result1, r)) {
cout << result1.str(0) << endl;//12234abc
cout << result1.str(1) << endl;//12234
cout << result1.str(2) << endl;//abc
cout << result1.str(10000) << endl;//空字符串,不会报错
cout << boolalpha << result1[0].matched << endl;//true
cout << result1[1].matched << endl;//true
cout << result1[2].matched << endl;//true
cout << result1[3].matched << endl;//false
}
string pattern2("([0-9]+)([a-z]+)");
regex r2(pattern2);
string str2 = "12234";
smatch result2;
regex_search(str2, result2, r2);
cout << result2[0].matched << endl;//false
cout << result2[1].matched << endl;//false
cout << result2[2].matched << endl;//false
string pattern3("([0-9]+)([a-z]+)?");//问号代表可选
regex r3(pattern3);
string str3 = "12234";
smatch result3;
regex_search(str3, result3, r3);
cout << result3[0].matched << endl;//true
cout << result3[1].matched << endl;//true
cout << result3[2].matched << endl;//false
system("pause");
}
regex_replace
regex_replace可以查找匹配正则表达式的字符串并使用其它格式来替换,符号$后跟子表达式的索引号表示一个特定的子表达式。
int main()
{
string pattern("([0-9]{4})-([0-9]{2})-([0-9]{2})");
regex r(pattern);
string date = "2019-06-28 2019-06-29";
cout << regex_replace(date, r, "$1.$2.$3") << endl;//2019.06.28 2019.06.29
system("pause");
}
默认情况下regex_replace会将未匹配的部分原样输出,我们可以通过指定format_no_copy不输出未匹配部分。
int main()
{
string pattern("([0-9]{4})-([0-9]{2})-([0-9]{2})");
regex r(pattern);
string date = "当前日期:2019-06-28";
cout << regex_replace(date, r, "$1.$2.$3",regex_constants::format_no_copy) << endl;//2019.06.28
system("pause");
}