79 points · by atroche · 11y ago · 43 comments
Automatic Generation of Regular Expressions from Examples with Genetic Programming[0]
Automatic String Replace by Examples [1]
[0] http://machinelearning.inginf.units.it/publications/internat...
[1] http://machinelearning.inginf.units.it/publications/internat...
we are experiencing some trouble with connectivity due to the... unexpected popularity :)
we are working to solve this, but we are a very small research group with limited resources.
meanwhile, if you are interested in the details of our algorithm take a look at the source code hosted here: https://github.com/MaLeLabTs/RegexGenerator
{a, b, c, d}
The most restrictive regular expression is: a | b | c | d
The most permissive regular expression is: *
If the system produces anything in between I cannot predict the output without knowing a set of rules that are more complicated than the regular expression that the system of rules produces. TANSTAAFL.[a-d]
I start to think that we are talking about different topics ;-)
Negative examples constrain the most permissive interpretation to the regular language defined:
1a. Let NegativeExamples be the most restrictive regular
expression [as above] using the negative examples as
input.
or
1b. Let NegativeExamples be the most permissive regular
expression [as above] using the negative exmaples as
input.
2. Let U be the regular language defined by the kleene star.
The negative examples define the regular language: U - NegativeExamples
And depending on the choice of 1a or 1b, you get a regular language slightly more restricted than the kleene star or the empty language.Regular expressions are a compact way of precisely describing an enumeration of permutations. The only way to get equal precision via enumeration is to fully enumerate all the permutations. Once a parser gets into the picture, then the input strings have to have semantics that the parser can use and a hidden set of parser rules for interpreting those semantics.
However, it could also have come up with the rule ([0-8]*).
So how does it know which one to choose? Can one also submit counter-examples?
Since it's impossible to do accurately, it has to use some "common sense" via genetic programming or whatever. In your example, it probably sees that people rarely want [1-8]{1,3}, but that it's a subset of \d+ and goes with that instead.
The result is: \d
Zipcodes would be something I'd like to see available as regexes.
(Edit: If you want the full ZIP+4 where it's given, you can add (\-\d{4})? as a term in the regex, and you can validate them the same way. USPS also offers an API for validating address information; it's been some years since I used it, and I recall it being somewhat recondite in the fashion of most .gov APIs, but it's there and it works.)
Edit again: OK, the site loaded. It looks like a tool for generating regexes via machine learning techniques to match highly complex targets. From a purely technical perspective, that sounds really neat -- but from a maintainability perspective, it sounds like even more of a nightmare than complex regexes usually are.