如何保存/序列化编译的正则表达式(std :: regex)到文件? [英] How to save/serialize compiled regular expression (std::regex) to a file?

查看:463
本文介绍了如何保存/序列化编译的正则表达式(std :: regex)到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visal Studio 2010中的< regex>
我理解当我创建regex对象然后编译。没有像其他语言和库中的 compile 方法,但我认为这是它的工作,我是对吗?

I'm using <regex> from Visal Studio 2010. I understand that when I create regex object then it's compiled. There is no compile method like in other languages and libraries but I thinks that's how it work, am I right?

我需要存储大这个编译的正则表达式在一个文件中的数量,所以我只是得到大块的内存块,并得到我编译的正则表达式。

I need to store large amount of this compiled regexes in a file so I would just get chunk of memory block and get my compiled regex.

我无法想象如何做到这一点。我发现在PCRE是可能,但它是Linux库。有一个Windows [版本 2 ,但它是3岁,我想使用更多高级方法(在Windows版本中没有c ++ wrapper)。

I can't figure how to do this. I found that in PCRE it is possible but it's Linux library. There is a Windows [version2 but it's 3 years old and I would like to use more high-level approach (there isn't c++ wrapper in windows version).

所以可以使用save std:regex boost :: regex (它是相同的权利?)作为一块内存,然后只是重用它吗?

So is it possible to use save std:regex or boost::regex (it's the same right?) as a chunk of memory and then simply reuse it later?

还是有其他简单的Windows库图书馆允许这样做?

Or is there other simple library for Windows that allows to do this?

编辑:伟大的答案。我将简单地检查是否足够简单地将正则表达式存储为字符串,然后如果它仍然是慢的,我将测试和比较它与这个旧的PCRE库。

Thanks for great answers. I'll simply check if it would be sufficient to simply store a regex as a string and then if it would still be slow I'll test and compare it with this old PCRE library.

推荐答案

我不认为无法修改boost库来支持它。

I don't think it can be done without modifying the boost library to support it.

知道如何实现boost regex库,但大多数regex库将事物编译成二进制blob,然后将其解释为一系列有限虚拟机的一系列指令。

I don't know specifically how the boost regex library is implemented, but most regex libraries compile things to a binary blob that's then interpreted later as a series of instructions for a sort of limited virtual machine.

如果boost的regex库以这种方式实现,序列化将是相对容易的。只是得到二进制blob不知何故,并将其转储到磁盘。 POSIX regex API的存在性的boost库告诉我这可能是如何实现。

If boost's regex library is implemented in this way, serializing it would be relatively easy. Just get at the binary blob somehow and dump it to disk. The existence of the POSIX regex API for the boost library tells me that this is probably how it's implemented.

OTOH,另一种实现它的方式(并不常见的方式)是生成类似正则表达式的抽象语法树。这意味着正则表达式的各个部分将由它们自己的对象表示,并且这些对象将被链接在一起成为代表整个正则表达式的更大的结构。

OTOH, another way to implement it (and a not so common way) is by generating something like an abstract syntax tree for the regex. This means that the individual pieces of the regex would be represented by their own objects and those objects would be linked together into some larger structure that represented the whole regex.

如果boost是这样的,那么序列化将是非常复杂的。

If boost does it this way then serialization will be very complex.

这是不可能与C + +,但我真正希望发生的是boost可以编译常量字符串正则表达式在编译时间与模板元编程。这是不可能的原因是,不可能用一个模板来迭代字符串的内容(即使是一个常量字符串)。

This is not possible with C++, but what I really wish happened is that boost could compile constant string regular expressions at compile time with template meta-programming. The reason this is not possible is that it isn't possible to iterate over the contents of a string (even a constant string) with a template.

这篇关于如何保存/序列化编译的正则表达式(std :: regex)到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆