Is there any relatively simple way to recognize Regex pattern as simple text or as a rule?
One example. @'[A-Z0-9]' – is a rule, and @'\\[A-Z0-9\\]' is a plain simple text (C# string syntax)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Short of detecting ‘ [‘ i.e. escaped regular expression characters, I can’t think of anything off the top of my head.
Since using @ copies the string literal verbatim try –
1.Finding every ‘[‘/’*’/’+’/’all other regex charachters’ and ensuring that the previous charachter is a ‘\’.
2.Another way might be to write a grammar/regex to check this. I think you’ll need a grammar. In any case (1) above is simpler.
Some string methods may help check for the above (like IndexOf …).
In any case, a regex is just a string, unless and until it is compiled and used. I still cant see why you want to do this. If you want to ensure something is a well formed regex, that is easier. (I don’t know a C# specific way for it offhand — See Svish’s answer.).
Note
1.A regex is always a rule because the regex ‘XYZ’ is a rule that always matches ‘XYZ’ alone and doesn’t match anything else.
I hope this helps.