Skip to main content

A little tool to help with Java regular expressions

Posted by kcpeppe on October 27, 2011 at 9:27 AM PDT

 Over the last few weeks I've been working Java regular expressions into a couple of applications. After a couple of rounds with trying to sort out regular expression syntax I hacked together this handy little GUI. If anyone makes any improvements do let me know and I'll be happy to make use of them.

 
package com.kodewerk.util;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * @author kirk
 * @since 5:54:30 AM, Sep 10, 2011
 */
public class TestPattern extends JFrame {
 
    private final JTextArea testString = new JTextArea();
    private final JTextArea testPattern = new JTextArea();
    private final JTextArea output = new JTextArea();
    private JButton findButton;
    private JButton groupButton;
    private JButton matchButton;
 
    public TestPattern() {
        super.setTitle("Java Regular Expression Evaluator");
        super.setLayout(new BorderLayout());
        JPanel listPane = new JPanel();
        listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
        //listPane.setBorder(BorderFactory.createEmptyBorder(30, 70, 10, 70));
        listPane.add(buildTestStringPanel());
        listPane.add(buildExpressionStringPanel());
        //listPane.add(buildSettingsPanel());
        listPane.add(buildResultsPanel());
        listPane.add(buildButtonsPanel());
        super.add(listPane, BorderLayout.CENTER);
    }
 
    private JPanel buildButtonsPanel() {
        JPanel buttons = new JPanel();
        buttons.setOpaque(false);
        findButton = new JButton("find");
        findButton.setBackground(Color.GREEN);
        buttons.add(findButton);
 
        groupButton = new JButton("group");
        groupButton.setBackground(Color.GREEN);
        buttons.add(groupButton);
 
        matchButton = new JButton("match");
        matchButton.setBackground(Color.RED);
        buttons.add(matchButton);
 
        JButton exitButton = new JButton("Exit");
        buttons.add(exitButton);
 
        findButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                executeFind();
            }
        });
 
        groupButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                executeGroup();
            }
        });
 
        matchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                executeMatch();
            }
        });
 
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
 
        return buttons;
    }
 
    private JPanel buildResultsPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(new JLabel("Regular Expression Evaluation Result"));
        output.setRows(10);
        output.setEditable(false);
        panel.add(new JScrollPane(output));
        return panel;
    }
 
    private JPanel buildTestStringPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(new JLabel("Test String"));
        testString.setRows(1);
        testString.setEditable(true);
        panel.add(new JScrollPane(testString));
        return panel;
    }
 
    private JPanel buildExpressionStringPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(new JLabel("Regular Expression"));
        testPattern.setRows(1);
        testPattern.setEditable(true);
        panel.add(new JScrollPane(testPattern));
        return panel;
    }
 
    private JPanel buildSettingsPanel() {
        JLabel label;
        JPanel settingPanel = new JPanel(new GridLayout(0, 2, 10, 10));
 
        settingPanel.setBorder(BorderFactory.createTitledBorder("Settings:"));
        settingPanel.setOpaque(false);
 
        label = new JLabel("Test String:", JLabel.RIGHT);
        label.setToolTipText("String to match pattern against");
        settingPanel.add(label);
        settingPanel.add(testString);
 
        label = new JLabel("Regular Expression:", JLabel.RIGHT);
        label.setToolTipText("regular expression to match against test string");
        settingPanel.add(label);
        settingPanel.add(testPattern);
 
        return settingPanel;
    }
 
    public void executeFind() {
        String pattern = testPattern.getText();
        String targetString = testString.getText();
        try {
            if ( Pattern.compile( pattern).matcher( targetString).find())
                output.setText( "pattern matched");
            else
                output.setText( "pattern failed to match");
        } catch(Throwable t) {
            StringBuilder text = new StringBuilder(t.getClass().getName());
            text.append('\n').append( t.getMessage());
            output.setText( text.toString());
        }
    }
 
    public void executeGroup() {
        String pattern = testPattern.getText();
        String targetString = testString.getText();
        try {
            Pattern p = Pattern.compile( pattern);
            Matcher m = p.matcher( targetString);
            String group = "";
            if ( m.find()) {
                int groups = m.groupCount();
                StringBuffer results = new StringBuffer();
                for ( int i = 0; i <= groups; i++) {
                    results.append(i + ": `" + m.group(i) + "`\n" );
                }
                output.setText(results.toString());
            }
        } catch(Throwable t) {
            StringBuilder text = new StringBuilder(t.getClass().getName());
            text.append('\n').append( t.getMessage());
            output.setText( text.toString());
        }
    }
 
    public void executeMatch() {
        String pattern = testPattern.getText();
        String targetString = testString.getText();
        try {
            if ( Pattern.compile( pattern).matcher( targetString).matches())
                output.setText( "pattern matched");
            else
                output.setText( "pattern failed to match");
        } catch(Throwable t) {
            StringBuilder text = new StringBuilder(t.getClass().getName());
            text.append('\n').append( t.getMessage());
            output.setText( text.toString());
        }
    }
 
    public static void main(String[] args) {
        final TestPattern me = new TestPattern();
        me.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        me.addWindowListener( new WindowAdapter() {
            public void windowClosing( WindowEvent evt) {
                int response = JOptionPane.showConfirmDialog( me, "Exit?\n",
                  "Exit?", JOptionPane.YES_NO_OPTION);
                if ( JOptionPane.YES_OPTION == response)
                    System.exit(0);
            }
        });
        me.pack();
        me.setVisible(true);
    }
}
 
On an upbeat note, Ecetra and I have agreed to a terms that will have them hosting my performance tuning course in Australia at the beginning of Feb (2012). Really looking forward to spending time in Sydney.

 

Related Topics >>

Comments

&nbsp;This is really neat. And, if I may suggest (shameless ...

 This is really neat. And, if I may suggest (shameless plug) a natural next step - how to effortlessly combine regular expressions into parsers - take a look at VisualLangLab. An executable jar is available here: VLLS-All.jar

I swear by this tool for my regex work (Java applet) which ...

I swear by this tool for my regex work (Java applet) which gives a live preview, includes tools for escaping to/from Java strings and tooltips on the regex identify group ids:

http://myregexp.com/