lua自带的字符串匹配方案并不完整支持正则表达式,无法匹配一些带有分支的模式,比如abc|123这种。
进过研究发现有两种简单的方案:
方案1)借助python、pipe分析数据
新建一个python脚本aaa.py,分析输入参数并输出结果
import re
import sys
for m in re.finditer("abc|123",len(sys.argv)>1 and sys.argv[1] or "",re.S):
print("\""+m.group(0)+"\",")
注意,这里python中将输出结果表示为字符串形式
lua中将源字符串传入python,再将返回结果通过loadstring整合成table数据
f=io.popen("python aaa.py abcdef1234567")
loadstring("t={"..f:read("*a").."}")()
for i,v in ipairs(t) do
print(v)
end
方案2)导出boost regex到lua
- 使用boost regex做正则表达式
- 使用luabind绑定接口
- 使用boost range、boost shared container iterator,返回多项匹配结果,并维护生存期
宿主程序C++代码
#include <boost/shared_container_iterator.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/regex.hpp>
#include <luabind/luabind.hpp>
#include <luabind/iterator_policy.hpp>
typedef std::vector<boost::cmatch> sub_match_list;
typedef boost::shared_container_iterator<sub_match_list> shared_sub_match_list_iter;
static boost::iterator_range<shared_sub_match_list_iter> regex_search_all(boost::regex* self, const char* s)
{
boost::cmatch match;
boost::shared_ptr<sub_match_list> matchs(new sub_match_list());
for (; boost::regex_search(s, match, *self, boost::match_default); s = match[0].second)
{
matchs->push_back(match);
}
return boost::make_iterator_range(shared_sub_match_list_iter(matchs->begin(), matchs), shared_sub_match_list_iter(matchs->end(), matchs));
}
static bool cmatch_is_matched(boost::cmatch* self, int i)
{
return self->operator[](i).matched;
}
static std::string cmatch_sub_match(boost::cmatch* self, int i)
{
return self->operator[](i);
}
luabind::module(L) [
, class_<boost::regex>("regex")
.def(constructor<const char *>())
.def("search_all", ®ex_search_all, luabind::return_stl_iterator)
, class_<boost::cmatch>("cmatch")
.def("is_matched", &cmatch_is_matched)
.def("sub_match", &cmatch_sub_match)
];
lua中使用regex举例
r=regex("abc|123")
for m in r:search_all("abcdef1234567") do
print(m:sub_match(0))
end