jrexx - automaton based regluar expression API for Java
D E M O O N E
This Java applet demonstrates the 'contains' method of the jrexx
Pattern class, which checks if a given text argument matches
the pattern's regular expression.
Instructions
- Insert a regular expression
(see regex syntax)
into the combobox.
- Insert an input text into the left text area
- Click the 'Check input' button
- The result will be shown in the right text area
D E M O T W O
The second Java applet demonstrates the use of the internal automaton
class for a more sophisticated task. This applet checks the input text
on every text change event (e.g. key press) and optionally blocks the
change, if the resulting text doesn't match the regular expression.
A detailed
description can be found below.
Instructions
- Click on the 'define' button and define a regular expression
(see regex syntax).
- Insert an input text into the textarea
- Optionally toggle the 'block invalid keys' checkbox
- See how the textarea's background switches the color to
indicated if the input text is valid (white) or invalid (pink)
Description
The demo above has two diffferent features which are implemented
separately.
- Changing colors
This is easy to achive. We just have to call the Pattern's
contains() method everytime after the input text has changed.
If the result is true, which means that the input text matches
the regular expression, we set the background of the textarea to white,
otherwise to pink.
- Blocking keys
When you play with the demo, you'll see, that you normally can't
insert an invalid text while the checkbox is checked.
But you are still able to delete any characters from
the text even although the resulting text might become invalid (which is
really a feature). Also you might notice, that the algorithm just checks
the input text between index 0 and the cursor position.
To achive this,
the following method is called on every text insertion and returns
the number of characters from the given text, that can be processed by
the pattern's automaton. Please note, that this does not mean, that
these characters form a valid String that matches the complete regular
expression.
Even if the whole text's length is returned by the method, the text still
might be invalid, because the regular expression might still miss
characters at the end of the text. If you want, you can say, that the
text's substring (from index 0 to the method's resulting int) still has
a chance to become a valid string (in the sense of the regex), just by
appending valid characters.
protected int getMatchLength( String text) {
if ( this.pattern == null) return 0;
final int textLength = text.length();
IState state = this.pattern.getAutomaton().getStartState().visit();
for (int i=0; i<textLength; ++i) {
state = state.next( text.charAt(i));
if (state==null) return i;
}
return textLength;
}
|
|
D E M O T H R E E
Since February 12th we also have jrexx-Lab online. This is a graphical
editor for automaton based regular expressions build upon the jrexx api.
Don't miss the
jrexx-Lab Java Webstart page.