Jul 31, 2001, 5:12 AM
Post #4 of 4
If I'm understanding correctly, you want to derive a regexp from a string. Or you wonder why no one has done so before.
The simple answer is "you can't". You can write a program to generate a regexp that would match a given string but that's trivially simple, just use the same string (after doing proper escaping) and you have a regexp that matches this string. Example:
$string = "Hello";
my $regexp = "/\Q$string\E/"; # this would match only the string above
my $regexp = "//"; # and this would match the string above
Do you see the problem there? There are (countably) infinite number of regular languages that accept a given input. A program can just enumerate all possible regular expressions that could match the given string. It's you, the programmer, who can tell which one should be chosen. Regular languages (and expressions) are just a very concise and practical way of describing what should or should not be matched.
Hope this clarifies,,,