S
S J Kissane
Hi all
I was thinking about regular expressions, in the context of
syntax checking in user interfaces.
Example use case: I have a form field, with a regex to
determine if its contents is valid. The user starts typing it
in... if its valid, the field goes green. If its invalid, but by
typing more they could make it valid, the field goes yellow.
If its invalid, and they cannot make it valid by typing more,
only by removing characters they've already typed, it
goes red.
Suppose I have a regular expression defined like this: [0-9A-F]{8}
Now, suppose the user has typed: "09AB"
We can see, although that string does not match the regular
expression,
it could match if the user added to it appropriately.
Comparatively, suppose they typed: "09AG"
We can see, that no matter what they possibly add,
it can never be made to match the regular expression;
the only way of making it match is to remove characters.
We might say that, although the string does not match the
regular expression, it is a "valid prefix" of the regular expression.
Now, the question is, given a regular expression and a string,
how in Java can I determine if the string is a valid prefix of
the regular expression? I have looked at the java.util.regex.Matcher
API in Java SE 6, and I can't see a way of doing this.
I suppose, if I wrote my own regular expression library
(or even just started with Sun's one and hacked it),
I could make this work... but I don't want to do that.
Thanks
Simon
I was thinking about regular expressions, in the context of
syntax checking in user interfaces.
Example use case: I have a form field, with a regex to
determine if its contents is valid. The user starts typing it
in... if its valid, the field goes green. If its invalid, but by
typing more they could make it valid, the field goes yellow.
If its invalid, and they cannot make it valid by typing more,
only by removing characters they've already typed, it
goes red.
Suppose I have a regular expression defined like this: [0-9A-F]{8}
Now, suppose the user has typed: "09AB"
We can see, although that string does not match the regular
expression,
it could match if the user added to it appropriately.
Comparatively, suppose they typed: "09AG"
We can see, that no matter what they possibly add,
it can never be made to match the regular expression;
the only way of making it match is to remove characters.
We might say that, although the string does not match the
regular expression, it is a "valid prefix" of the regular expression.
Now, the question is, given a regular expression and a string,
how in Java can I determine if the string is a valid prefix of
the regular expression? I have looked at the java.util.regex.Matcher
API in Java SE 6, and I can't see a way of doing this.
I suppose, if I wrote my own regular expression library
(or even just started with Sun's one and hacked it),
I could make this work... but I don't want to do that.
Thanks
Simon