regex_test.cpp -- learning boost.regex

boost.regex 库的用法,看来这可能是 boost 当中写法最“常规”的库之一了。

regex_test.cpp:

#include <string>
#include <map>
#include <fstream>
#include <iostream>
#include <boost/regex.hpp>

using namespace std;

// purpose:
// takes the contents of a file in the form of a string
// and searches for all the C++ class definitions, storing
// their locations in a map of strings/int's

typedef map<string, string::difference_type, less<string> > map_type;

const char* re =
// possibly leading whitespace:
"^[[:space:]]*"
// possible template declaration:
"(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
// class or struct:
"(class|struct)[[:space:]]*"
// leading declspec macros etc:
"("
"\\<\\w+\\>"
"("
"[[:blank:]]*\\([^)]*\\)"
")?"
"[[:space:]]*"
")*"
// the class name
"(\\<\\w*\\>)[[:space:]]*"
// template specialisation parameters
"(<[^;:{]+>)?[[:space:]]*"
// terminate in { or :
"(\\{|:[^;\\{()]*\\{)";

boost::regex expression(re);
map_type class_index;

bool regex_callback(const boost::match_results<string::const_iterator>& what)
{
// what[0] contains the whole string
// what[5] contains the class name.
// what[6] contains the template specialisation if any.
// add class name and position to map:
class_index[what[5].str() + what[6].str()] = what.position(5);
return true;
}

void load_file(string& s, istream& is)
{
s.erase();
s.reserve(is.rdbuf()->in_avail());
cout << s.capacity();
char c;
while(is.get(c))
{
if(s.capacity() == s.size())
s.reserve(s.capacity() * 3);
s.append(1, c);
}
}

int main(int argc, const char** argv)
{
string text;
for(int i = 1; i < argc; ++i)
{
cout << "Processing file " << argv[i] << endl;
ifstream fs(argv[i]);
load_file(text, fs);
// construct our iterators:
boost::sregex_iterator m1(text.begin(), text.end(), expression);
boost::sregex_iterator m2;
for_each(m1, m2, &regex_callback);
// copy results:
cout << class_index.size() << " matches found" << endl;
map_type::iterator c, d;
c = class_index.begin();
d = class_index.end();
while(c != d)
{
cout << "class \"" << (*c).first << "\" found at index: " << (*c).second << endl;
++c;
}
class_index.erase(class_index.begin(), class_index.end());
}

return 0;
}

//========== For Test =============
class Person
{};

template <class T>
class Temp
{
};

template <>
class Temp<string>
{
};

=================================================================================
cl /EHsc regex_test.cpp

regex_test regex_test.cpp

OUTPUT:

Processing file regex_test.cpp
153 matches found
class "Person" found at index: 2331
class "Temp" found at index: 2368
class "Temp<string>" found at index: 2397

关于“libboost_regex-vc100-mt-s-1_53.lib”,虽未找到其详细功能介绍,但可以从其他相关信息推断一些内容。 “libboost_regex”表明这是 Boost 库中用于正则表达式处理的库,正则表达式库通常用于字符串的模式匹配、查找、替换等操作。“vc100”代表该库是为 Visual Studio 2010(Visual C++ 10.0)编译的;“mt”表示使用多线程运行时库;“s”可能表示静态链接;“1_53”代表 Boost 库的版本是 1.53。 在使用方面,链接时需要确保链接器能够找到该库文件。在命令行中,通常需要指定 `/libpath` 和 `/lib` 选项来指定库文件路径和库文件本身 [^1]。 若在链接过程中出现类似“无法打开文件”的错误,可参考解决“libboost_regex-vc120-mt-gd-1_59.lib”链接失败的方法。可能是电脑里没有安装 Boost v1.53,或者安装的版本不对,也可能是工程里的 lib 路径没有设置好。可先使用 everything 软件搜索该 lib 文件是否存在,若不存在,可到 source forge 官网下载与对应编译器匹配的 Boost C++ Libraries v1.53 版本 [^3]。 另外,还可以参考 Visual Studio 编译 C++ 时报错“无法打开文件‘libboost_XXX-vcX-mt-gd-X_XX.lib’”的解决办法。在 Visual Studio 中进行项目属性设置,包含附加的库目录等操作 [^4]。 ### 代码示例 以下是一个简单的使用 Boost 正则表达式库的示例代码: ```cpp #include <iostream> #include <boost/regex.hpp> int main() { std::string input = "Hello, World!"; boost::regex pattern("Hello"); if (boost::regex_search(input, pattern)) { std::cout << "Pattern found!" << std::endl; } else { std::cout << "Pattern not found." << std::endl; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值