3

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

Is there an "easy" way to do this, or does something like this already exist?

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

Edit 2: Regulazy is not what I am looking for. The common use for the code I want is to find a correct RegEx; for example, article numbers:

  • I put in 123456, the regex should be \d{6}
  • I put in nb-123456, the regex should be \w{2}-\d{6}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
gamue
  • 161
  • 1
  • 8
  • IMO, the solutions to this problem will not be predictive, it is like finding factors for a given number. The input data still plays a major role in the output. For ex: given an input 'list' we can have .*, \w*, \w+, \w{4}, \w{2}\w{2}... (yes, wierd but possible). Interesting question though. – questzen Oct 07 '08 at 10:52
  • Answer to [Generate RegEx from matches](http://stackoverflow.com/a/12068327/471214) – mmdemirbas Aug 30 '12 at 20:31

7 Answers7

3

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"
David Webb
  • 190,537
  • 57
  • 313
  • 299
2

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
  • 98,437
  • 31
  • 224
  • 236
2

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
  • 50,406
  • 14
  • 85
  • 110
1

How about the following (matches every string)?

.*

E..
  • 29
  • 1
  • 5
1

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
  • 5,881
  • 4
  • 25
  • 24
0

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
  • 1,801
  • 15
  • 14
0

Look at txt2re.

This site holds a form that takes a sample string and generates a regex pattern that can match the given string.

Then it generates the corresponding script for the following languages: Perl, PHP, Python, Java, Javascript, ColdFusion, C, C++ Ruby, VB, VBScript, J#.net, C#.net, C++.net, VB.net

Seki
  • 11,135
  • 7
  • 46
  • 70