Walt Mankowski on 21 Oct 2009 11:45:30 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] PCRE is_a_regex?


On Wed, Oct 21, 2009 at 03:41:54AM -0400, JP Vossen wrote:
> I have a problem with Perl and regular expressions (yeah, I know, now I 
> have two problems:).
> 
> I need to know whether a variable contains a static string or something 
> that is a valid regular expression.  One problem is that a static string 
> is also a regular expression but I need to exclude that case, so 
> evaluating the results of a qr//; doesn't help.
> 
> To make it even more fun, the strings will sometimes contain escaped 
> PCRE meta characters or valid literal backslashes, so I can't just m/\\/;
> 
> my $regex     = '\ This\sone\s*should\s+flag as a regex \ ';
> my $not_regex = '\ This one should not flag as a regex. \ ';
> 
> 
> The only idea I have right now would be a nightmare regex using negative 
> lookbehinds to try to ID various unescaped PCRE meta characters.  And 
> even typing that gives me a headache.
> 
> Any clues,

The way you describe it, it sounds impossible.  As you said, every
normal string is already a valid regex, and many regex sequences can
occur normally within strings.  Short of writing your own regex
parser, it seems like the best you can do is search for some common
patterns that occur in regexes.

But I'm not really clear why you need to do this in the first place.
Since static strings are valid regular expressions, what's the harm in
just treating everything as a regex?  Also, since the users entering
these strings presumably knows whether or not they're supposed to be
regexes, isn't there any way you can get them to indicate it in the
data somehow?

Finally, I'm confused by your use of "PCRE".  Do you mean "perl
regular expressions", or the the PCRE library for "Perl Compatible
Regular Expressions"?

Walt

Attachment: signature.asc
Description: Digital signature

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug