I need to check if a given string matches a partially specified regular expression. For example, for regex, the ab[0-9]cstrings "a", "ab", "ab3" and "b3c" will "match", but not the strings "d", "abc" or "a3c". What I did was clumsy a(?:b(?:[0-9](?:c)?)?)?(which only works for partial matches, in particular those that “start” to match), but since this is part of the API, I would better give users a more intuitive way to enter their matching regular expressions.
In case the description is not very clear (and I understand that this may not be so!), This will be used to check text input in text fields. I want to prevent any editing that will result in an invalid string, but I can't just match the string to a regular regex, since until it is fully entered, it will not match. For example, using the regex above ( ab[0-9]c) when I try to type "a", it is not allowed because the string "a" does not match the regular expression.
Basically, it's a kind of converse startsWith()that works with regular expressions. ( new Pattern("ab[0-9]c").startsWith("ab3")must return true.)
Any ideas?
Tonio source
share