正则表达式是独立与任何语言本身的一个很大的话题。在C++中,regex就是涉及到正则表达式的内容。
[a-z]+.\txt:在这个正则表达式中,[a-z]标识匹配一个小写字母,+可以是前面的表达式匹配多次,因此[a-z]+能够匹配一个小写字母组成的字符串。
在正则表达式中的一个 . 表示匹配任意字符,而 . 则表示匹配字符 . ,最后txt表示严格匹配txt三个字母。因此这个正则表达式索要匹配的内容就是由纯小写字母组成的文本文件。
regex_match用于匹配字符串和正则表达式,有很多不同的重载形式。最简单一种形式就是传入string以及一个regex进行匹配,当匹配成功,会返回一个true,否则返回一个false。
/*
regex.cpp
*/
#include<iostream>
#include<string>
#include<regex>
using namespace std;
int main()
{
string fnames[] = {"foo.txt", "bar.txt", "test", "a0.txt", "AAA.txt"};
std::regex txt_regex("[a-z]+\\.txt");
for (const auto &fname: fnames)
{
cout << fname << ": " << regex_match(fname, txt_regex) << endl;
}
return 0;
}
编译:
g++ regex.cpp -o regex -std=c++11
运行结果:
foo.txt: 1
bar.txt: 1
test: 0
a0.txt: 0
AAA.txt: 0
另一种常用的形式就是依次传入 std::string/std::smatch/std::regex 三个参数,其中 std::smatch 的本质其实是 std::match_results,在标准库中, std::smatch 被定义为了 std::match_results
/*
regex_1.cpp
*/
#include <iostream>
#include <string>
#include <regex>
int main() {
std::string fnames[] = {"foo.txt", "bar.txt", "test", "a0.txt", "AAA.txt"};
std::regex base_regex("([a-z]+)\\.txt");
std::smatch base_match;
for(const auto &fname: fnames) {
if (std::regex_match(fname, base_match, base_regex)) {
// sub_match 的第一个元素匹配整个字符串
// sub_match 的第二个元素匹配了第一个括号表达式
if (base_match.size() == 2) {
std::string base = base_match[1].str();
std::cout << "sub-match[0]: " << base_match[0].str() << std::endl;
std::cout << fname << " sub-match[1]: " << base << std::endl;
}
}
}
}
运行结果:
sub-match[0]: foo.txt
foo.txt sub-match[1]: foo
sub-match[0]: bar.txt
bar.txt sub-match[1]: bar
原文链接: https://www.cnblogs.com/wanghao-boke/p/12239945.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/192467
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!