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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.
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.
For instance, the string
'2009/11/12'would be recognized as ayyyymmddpattern, which is helpful. The tool transforms it into this 125 character monster:The hand-made equivalent would take up merely two fifths of that (50 characters):