tags:

views:

200

answers:

7

Something like this is in my mind: I put one or a few strings in and the algorithm shows me a matching regex.

Is there any "easy" way to do this, or something like this already exists?

Edit: Yes, I'm trying to find a way to generate regex.

Edit: Regulazy is not the thing i'm searching for. The common use for the code i want is to find a correct regex for example articlenumbers.
Example:

  • I put in 123456, the regex should be \d{6}
  • I put in nb-123456, the regex should be \w{2}-\d{6}
+2  A: 

It sounds like you want an algorithm to generate a regular grammar based on some samples. In a lot of cases, there are many possible grammars for a given set of examples--there can even be infinite possible grammars. Of course, the possibilities can be limited by a second set of required non-matches, which can limit it to zero possibilities if the non-matching strings are too inclusive.

txt2re does something like this.

Mark Cidade
txt2re looks there to me! :-)
Ken
+1  A: 

How about the following (matches every string)?

.*

E.
+3  A: 

If you have Emacs you can use regexp-opt. For example, evaluating:

(regexp-opt (list "my" "list" "of" "some" "strings" "to" "search"))

returns

"list\\|my\\|of\\|s\\(?:earch\\|ome\\|trings\\)\\|to"
Dave Webb
+1  A: 

I think that Regulazy by Roy Osherove does this to a certain extent, or it may be Regulator. BOth are on this page:

http://weblogs.asp.net/rosherove/pages/tools-and-frameworks-by-roy-osherove.aspx

Carl
+2  A: 

Perl can do it: http://www.hakank.org/makeregex/

So does ruby: http://www.toolbox-mag.de/data/makeregex.html

Note: not so perfect solution.

And there is a CLI tool: txt2regex.

There was txt2re, once upon a time...

Zsolt Botykai
A: 

if your input strings are not random strings and they are based on some rules, by using a parser (i.e. jflex), you can create a regex generator which will generate a regex w.r.t. the given strings.

hakan
A: 

txt2re.com is back