Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
a*b
b
ab
aab
aaab
aaaa...b
...
a*b
Pattern
(ab)*b
b
abb
ababb
abababb
abababab...b
...
(ab)*b
[ab]*b
b
ab
bb
aab
abb
bbb
...
aabbaab
...
[ab]*b
[a-z]
[^xyz]
[a(de)]*b
b
ab
deb
adeb
deab
deadeb
...
[a(de)]*b
\d*
\d*\.+\d*
import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;
import javax.swing.*;
import javax.swing.event.*;
public class RegularExpressionTester {
public static void main (String [] args) {
final JTextField patternField = new JTextField (12);
final JTextField testField = new JTextField (12);
patternField.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
String pattern = patternField.getText ();
try {
Pattern.compile (pattern);
patternField.setBackground (Color.GREEN);
}
catch (PatternSyntaxException exception) {
patternField.setBackground (Color.RED);
}
}
});
testField.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event) {
String pattern = patternField.getText ();
String string = testField.getText ();
//Check if input matches regex String
if (string.matches (pattern)) {
testField.setBackground (Color.GREEN);
}
else {
testField.setBackground (Color.RED);
}
}
});
final JFrame frame = new JFrame ("Regular Expression Tester");
frame.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent event) {
System.exit (1);
}
});
Container contentPane = frame.getContentPane ();
contentPane.setLayout (new GridLayout (2, 2, 0, 0));
contentPane.add (new JLabel ("Pattern "));
contentPane.add (patternField);
contentPane.add (new JLabel ("Test String "));
contentPane.add (testField);
frame.pack ();
frame.show ();
}
}