Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating/deducing regular expressions

Status
Not open for further replies.

MacTommy

Programmer
Feb 26, 2007
116
0
0
NL
I am totally aware of the fact that it is not a really solvable task, but I would like to make (well..., have) something that generates a regular expression based on 2 or more strings. Or the most strict regular expression possible or something...

So if you would feed it "a123" and "345b" it should return something like /a?\d{3}b?/.

There is a plethora of very difficult problems that you'll run into of course.
Like e.g. in the above case /[a3][14][25][3b]/ would be stricter. And at the other side of the spectrum /.*/ will always work.

However it can't be the case that I am the first one thinking about this.

Does anybody have any starting points like links/pointers to literature about this..?!?
 
Seems your goal is to find a regex matching on all the supplied strings and not matching on any other string. But what's the purpose?
You should also define any restricting rules: e.g. strings with equal lengths, allowed characters, regex having a minimum length...
With no rules defined, you already showed a systematic way of finding a regex satisfying the goal (your second example).

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Yeah, you are right of course. Without any restrictions you don't know where you can be a bit looser and where you should be somewhat more strict.
But I thought, maybe some smart people have already thought about what kind of restrictions make most sense (depends on what you want obviously, but maybe there are some general rules of thumb...?!?).
 
You're essentially talking about an expert system. You feed it with strings which should, and should not be accepted. Over time it builds up rules which enable it to determine what it should do with any string you give it. The internal representation of those rules is unlikely to be a neat little regex though.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top