给定一个字符串,生成一个可以解析*类似*字符串的正则表达式 [英] Given a string, generate a regex that can parse *similar* strings
问题描述
例如,给定字符串2009/11/12我想得到正则表达式(\d {2} / d {2} / d {4}),所以我将能够也匹配2001/01/02。
For example, given the string "2009/11/12" I want to get the regex ("\d{2}/d{2}/d{4}"), so I'll be able to match "2001/01/02" too.
有什么能做到的吗?相似的东西?任何想法'如何做到这一点?
Is there something that does that? Something similar? Any idea' as to how to do it?
推荐答案
有 text2re ,一个免费的基于网络的正则表达式生成器。
There is text2re, a free web-based "regex by example" generator.
我不认为这是可用的但是在源代码中。我敢说没有自动正则表达式生成器可以在没有用户干预的情况下正确使用它,因为这需要机器知道你想要的东西。
I don't think this is available in source code, though. I dare to say there is no automatic regex generator that gets it right without user intervention, since this would require the machine knowing what you want.
请注意,text2re使用基于模板,模块化和非常通用的方法来生成正则表达式。它生成的表达式有效,但它们比同等的手工制作表达式复杂得多。它不是学习正则表达式的好工具,因为它在设置示例时做的很糟糕。
Note that text2re uses a template-based, modularized and very generalized approach to regular expression generation. The expressions it generates work, but they are much more complex than the equivalent hand-crafted expression. It is not a good tool to learn regular expressions because it does a pretty lousy job at setting examples.
例如,字符串2009/11/12
将被识别为 yyyymmdd
模式,这很有帮助。该工具将其转换为此 125个角色怪物:
For instance, the string "2009/11/12"
would be recognized as a yyyymmdd
pattern, which is helpful. The tool transforms it into this 125 character monster:
((?:(?:[1]{1}\d{1}\d{1}\d{1})|(?:[2]{1}\d{3}))[-:\/.](?:[0]?[1-9]|[1][012])[-:\/.](?:(?:[0-2]?\d{1})|(?:[3][01]{1})))(?![\d])
手工制作的等价物仅占五分之二(50个字符):
The hand-made equivalent would take up merely two fifths of that (50 characters):
([12]\d{3})[-:/.](0?\d|1[0-2])[-:/.]([0-2]?\d|3[01])\b
这篇关于给定一个字符串,生成一个可以解析*类似*字符串的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!