如何在 Google App Engine 数据存储区中存储正则表达式?
正则表达式通常表示为字符串,但它们也有属性(即单行、多行、忽略大小写)。 你会如何储存它们? 而对于编译好的正则表达式,如何存储呢?
请注意,我们可以编写自定义属性类:http:// googleappengine.blogspot.com/2009/07/writing-custom-property-classes.html
由于我对 Python 的了解不够,我第一次尝试编写一个存储已编译正则表达式的自定义属性失败了。
Regular Expressions are usually expressed as strings, but they also have properties (ie. single line, multi line, ignore case). How would you store them? And for compiled regular expressions, how to store it?
Please note that we can write custom property classes: http://googleappengine.blogspot.com/2009/07/writing-custom-property-classes.html
As I don't understand Python enough, my first try to write a custom property which stores a compiled regular expression failed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定Python是否支持它,但在.net正则表达式中,您可以在正则表达式本身中指定这些选项:
将指定单行,忽略大小写。
事实上,Python 文档在这里描述了这样的机制: http://docs.python.org/library /re.html
回顾一下:(从上面的链接剪切并粘贴)
(?iLmsux)
(来自集合“i”、“L”中的一个或多个字母, 'm', 's', 'u', 'x'。) 该组匹配空字符串; 这些字母设置相应的标志:re.I(忽略大小写)、re.L(取决于语言环境)、re.M(多行)、re.S(点匹配所有)、re.U(取决于 Unicode)和re.X(详细),用于整个正则表达式。 (这些标志在模块内容中进行了描述。)如果您希望将标志作为正则表达式的一部分包含在内,而不是将标志参数传递给compile()函数,那么这非常有用。
请注意, (?x) 标志更改表达式的解析方式。 它应该首先在表达式字符串中使用,或者在一个或多个空白字符之后使用。 如果标志前有非空白字符,则结果未定义。
I'm not sure if Python supprts it, but in .net regex, you can specify these options within the regex itself:
would specify single-line, ignore case.
Indeed, the Python docs describe such a mechanism here: http://docs.python.org/library/re.html
To recap: (cut'n'paste from link above)
(?iLmsux)
(One or more letters from the set 'i', 'L', 'm', 's', 'u', 'x'.) The group matches the empty string; the letters set the corresponding flags: re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), re.U (Unicode dependent), and re.X (verbose), for the entire regular expression. (The flags are described in Module Contents.) This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the compile() function.
Note that the (?x) flag changes how the expression is parsed. It should be used first in the expression string, or after one or more whitespace characters. If there are non-whitespace characters before the flag, the results are undefined.
您可以按照上面的建议存储文本,也可以 pickle 和 unpickle 已编译的 RE。 例如,请参阅说明书上的 PickledProperty。
由于 Pickle 的(缺乏)速度,特别是在 cPickle 不可用的 App Engine 上,您可能会发现存储正则表达式的文本是更快的选择。 事实上,看起来在腌制时,re 无论如何都只是存储原始文本。
You can either store the text, as suggested above, or you can pickle and unpickle the compiled RE. For example, see PickledProperty on the cookbook.
Due to the (lack of) speed of Pickle, particularly on App Engine where cPickle is unavailable, you'll probably find that storing the text of the regex is the faster option. In fact, it appears that when pickled, a re simply stores the original text anyway.
我不会尝试存储编译后的正则表达式。 已编译正则表达式中的数据并非旨在存储,并且不保证可picklable或可序列化。 只需存储字符串并重新编译(无论如何,re 模块都会在幕后为您执行此操作)。
I wouldn't try to store the compiled regex. The data in a compiled regex is not designed to be stored, and is not guaranteed to be picklable or serializable. Just store the string and re-compile (the re module will do this for you behind the scenes anyway).