Reputation: 773
In my program I allow an administrator to define what a particular string format should be in part of a template document. This may contain a regular expression, or it might just be a literal string.
If it's a literal string, then I want to copy the string as-is automatically for all instances in the document, but if it is a (PERL) regular expression, I want to prompt the user for the correct string. This part is executed by a lower level user (non-admin) who probably doesn't understand regexes, so I don't want to present one to them as a default to be filled in.
Currently I am searching for regex characters via a simple call in a loop through the string ala:
if ( strchr ( "\\^.$({[+-?*|", cInputP [ iLoop ] ) != NULL ) {
bRet = TRUE;
break; }
However this seems inelegant and prone to syntax mistakes. Is there an existing function in the PCRE that would do this? The pcre_study() functions don't seem to do this, and neither can I see how to get this info from pcre_compile().
Ideally I'd also like to be able to break the string up into components of literal sub-strings and user-entered sub-strings, so that the replacement string can be prefilled with the known required characters even if it does contain regexes. Is this possible with the PCRE library as-is, or do I need to extend it with my own code?
Upvotes: 1
Views: 204
Reputation: 399813
Your requirement is a bit confusing, since it implies a somewhat false division; in fact all strings are regular expressions, matching themselves (possibly excepting strings that contain invalid regular expressions, of course).
So it's a bit weird to try to detect if a string is a "literal string or a regular expression".
It might be better to let the user tell the program explicitly if a literal string is enough, or if using a regular expression is desired.
Upvotes: 2