I'm developing an application where users enter a regular expression as a filter criterion, however I do not want people to be (easily) able to enter .*
(i.e. match anything). The problem is, if I just use if (expression == ".*")
, then this could be easily sidestepped by entering something such as .*.*
.
Does anyone know of a test that could take a piece of regex and see if is essentially .*
but in a slightly more elaborate form?
My thoughts are:
I could see if the expression is one or more repetitions of
.*
, (i.e. if it matches(\.\*)+
(quotations/escapes may not be entirely accurate, but you get the idea). The problem with this is that there may be other forms of writing a global match (e.g. with$
and^
) that are too exhaustive to even think of upfront, let along test.I could test a few randomly generated Strings with it and assume that if they all pass, the user has entered a globally matching pattern. The problem with this approach is that there could be situations where the expression is sufficiently tight and I just pick bad strings to match against.
Thoughts, anyone?
(FYI, the application is in Java but I guess this is more of an algorithmic question than one for a particular language.)