lua使用完整正则表达式方案

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", &regex_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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值