I want to validate a some C# source code for a scripting engine. I want to make sure that only System.Math class members may be referenced. I am trying to create a regular expression that will match a dot, followed by a capital letter, followed by any number of word characters, ending at a word boundry that is NOT preceded by System.Math.
I started with this:
(?<!Math)\.[A-Z]+[\w]*
Which works fine for:
return Math.Max(466.89/83.449 * 5.5); // won’t flag this
return Xath.Max(466.89/83.449 * 5.5); // will flag this
It correctly matches .Max when it is not preceded by Math. However, now that I'm trying to expand the regular expression to include System, I can't get it to work.
I've tried these permutations of the regular expression and more:
((?<!System\.Math)\.[A-Z]+[\w]*)
((?<!(?<!System)\.Math)\.[A-Z]+[\w]*)
((?<!System)\.(?<!Math)\.[A-Z]+[\w]*)
((?<!System)|(?<!Math)\.[A-Z]+[\w]*)
((?<!System\.Math)|(?<!Math)\.[A-Z]+[\w]*)
Using these statements:
return System.Math.Max(466.89/83.449 * 5.5);
return System.Xath.Max(466.89/83.449 * 5.5);
return Xystem.Math.Max(466.89/83.449 * 5.5);
I've tried everything that I could think of, but it either ALWAYS matches the second element (.Math or .Xath above) or it DOESN'T match ANYTHING.
If anyone would have have mercy on me and point out what I'm doing wrong, I would greatly appreaciate it.
Thanks in advance, Welton