views:

181

answers:

3

Does anyone know of a library (preferably php) or algorithm for auto-generating regex's from some common descriptions?

For example, have a form with the possible options of:

  • Length (=x, between x & y, etc)
  • Starts with
  • Ends with
  • Character(s) x(yz) at index i
  • Specify one or more alternative behavior based on the above
  • And so on..

The idea is that for certain data entities in a system, you'll be able to go to a form and set this criteria for a data field. Afterward, any time that data field for that type of data entity is entered, it will be validated against the regex.

This seems like it could grow into a complex problem though, so I'm not expecting anyone to solve it as a whole. Any suggestions are much appreciated.

A: 

I have found txt2re, but it doesn't quite go all the way as it only generates single regex's and the whole site code might not be available.

Dana the Sane
+1  A: 

This doesn't take care of all of your issues but the PHP Filter functions may be a good starting place.

Mark Biek
A: 

Would simple globs be enough? For globs it's just a matter of replacing * with .* and adding ^ and $. Or may be Excel-style patterns? It should not be too hard to write a regexp generator for simple rules like this...

My point is, adjust your requirements to simplify the code, and then may be add more features as needed.

Arkadiy
Globs should be, but I didn't really want to have to write my own re code generator and some of the patters may be fairly complex. For your 2nd point, I wish I could simplify, but the validation is on input from many different entities who don't agree on universal formats; I'd like to support each.
Dana the Sane