regex

Swing GUI application for regular expression testing

This is an example of how to create a Swing GUI application for regular expression testing. The application creates a window that reads a pattern and a String and checks if the String matches the pattern and how many times the pattern appears in the String. The steps to create such an application are described below:

  • Class GuiDemo extends the JPanel and consists of a Pattern, a Matcher m, two JTextField pattTF and strTF, a JCheckBox compiledOK, three JRadioButton match, find, findAll and a JTextField mTF. It has four methods.
  • The method setMatch(boolean b) sets the JTextField mTF to yes or no, according to the boolean parameter.
  • The method setMatch(int n) sets the JTextField mTF to the String representation of the int parameter.
  • The method tryToCompile() creates a new pattern by compiling the JTextField pattTF String representation, with getText() API method of JTextComponent and then compile(String regex) API method of Pattern. Then it creates a Matcher to match the pattern with matcher(CharSequence input) API method of Pattern and it sets the JCheckBox compiledOK to true, using setSelected(boolean b) of AbstractButton. If a PatternSyntaxException is thrown then the compiledOK is set to false.
  • The method boolean tryToMatch() checks if the pattern is null, and if so it returns false, else it resets the matcher with the
    the JTextField strTF String, with getText() API method of JTextComponent. Then, if the JRadioButton match is set to true, with isSelected() API method of AbstractButton and the matcher matches against the pattern, with matches() API method of Matcher, then it calls the setMatch(boolean b) method of the class, with parameter set to true and returns true. If the JRadioButton find is set to true and the matcher finds the next input sequence that matches the pattern, with find() API method of Matcher it calls the setMatch(boolean b) method of the class, with parameter set to true and returns true. If the JRadioButton findAll is set to true then it finds the next subsequence of the input sequence that matches the pattern it increases an int variable and then calls setMatch(int n) method of class, and returns true.
  • The two JTextField pattTF and strTF are two classes PattListener and StrListener that both implement the DocumentListener, that is an interface for an observer to register to receive notifications of changes to a text document. Every time changedUpdate(DocumentEvent ev), insertUpdate(DocumentEvent ev), removeUpdate(DocumentEvent ev) are invoked the PattListener calls tryToCompile() method and StrListener calls tryToMatch() method.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.awt.GridLayout;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 * Standalone Swing GUI application for demonstrating REs. <br/>TODO: Show the
 * entire match, and $1 and up as captures that matched.
 */

public class GuiDemo extends JPanel {

    protected Pattern pattern;
    protected Matcher m;
    protected JTextField pattTF, strTF;
    protected JCheckBox compiledOK;
    protected JRadioButton match, find, findAll;
    protected JTextField mTF;

    /**
     * "main program" method - construct and show
     */
    public static void main(String[] av) {

  

  JFrame f = new JFrame("GuiDemo");

  

  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  GuiDemo comp = new GuiDemo();

  f.setContentPane(comp);

  f.pack();

  f.setLocation(200, 200);

  f.setVisible(true);
    }

    /**
     * Construct the REDemo object including its GUI
     */
    public GuiDemo() {

  super();


  JPanel top = new JPanel();

  top.add(new JLabel("Pattern:", JLabel.RIGHT));

  pattTF = new JTextField(20);

  pattTF.getDocument().addDocumentListener(new PattListener());

  top.add(pattTF);

  top.add(new JLabel("Syntax OK?"));

  compiledOK = new JCheckBox();

  top.add(compiledOK);


  ChangeListener cl = new ChangeListener() {



@Override


public void stateChanged(ChangeEvent ce) {


    tryToMatch();


}

  };

  

  JPanel switchPane = new JPanel();

  ButtonGroup bg = new ButtonGroup();

  match = new JRadioButton("Match");

  match.setSelected(true);

  match.addChangeListener(cl);

  bg.add(match);

  switchPane.add(match);

  find = new JRadioButton("Find");

  find.addChangeListener(cl);

  bg.add(find);

  switchPane.add(find);

  findAll = new JRadioButton("Find All");

  findAll.addChangeListener(cl);

  bg.add(findAll);

  switchPane.add(findAll);


  JPanel strPane = new JPanel();

  strPane.add(new JLabel("String:", JLabel.RIGHT));

  strTF = new JTextField(20);

  strTF.getDocument().addDocumentListener(new StrListener());

  strPane.add(strTF);

  strPane.add(new JLabel("Matches:"));

  mTF = new JTextField(3);

  strPane.add(mTF);


  setLayout(new GridLayout(0, 1, 5, 5));

  add(top);

  add(strPane);

  add(switchPane);
    }

    protected void setMatch(boolean b) {

  if (b) {


mTF.setText("Yes");

  } else {


mTF.setText("No");

  }
    }

    protected void setMatch(int n) {

  mTF.setText(Integer.toString(n));
    }

    protected void tryToCompile() {

  pattern = null;

  try {


pattern = Pattern.compile(pattTF.getText());


m = pattern.matcher("");


compiledOK.setSelected(true);

  } catch (PatternSyntaxException ex) {


compiledOK.setSelected(false);

  }
    }

    protected boolean tryToMatch() {

  if (pattern == null) {


return false;

  }

  m.reset(strTF.getText());

  if (match.isSelected() && m.matches()) {


setMatch(true);


return true;

  }

  if (find.isSelected() && m.find()) {


setMatch(true);


return true;

  }

  if (findAll.isSelected()) {


int i = 0;


while (m.find()) {


    ++i;


}


if (i > 0) {


    setMatch(i);


    return true;


}

  }

  setMatch(false);

  return false;
    }

    /**
     * Any change to the pattern tries to compile the result.
     */
    class PattListener implements DocumentListener {


  @Override

  public void changedUpdate(DocumentEvent ev) {


tryToCompile();

  }


  @Override

  public void insertUpdate(DocumentEvent ev) {


tryToCompile();

  }


  @Override

  public void removeUpdate(DocumentEvent ev) {


tryToCompile();

  }
    }

    /**
     * Any change to the input string tries to match the result
     */
    class StrListener implements DocumentListener {


  @Override

  public void changedUpdate(DocumentEvent ev) {


tryToMatch();

  }


  @Override

  public void insertUpdate(DocumentEvent ev) {


tryToMatch();

  }


  @Override

  public void removeUpdate(DocumentEvent ev) {


tryToMatch();

  }
    }
}

 
This was an example of Swing GUI application for regular expression testing in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button