java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4097
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to create a String pattern matcher E-mail
User Rating: / 16
PoorBest 

The Swing application written below takes text and a pattern as input and display whether the pattern was found in the text.

import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;
import javax.swing.*;

public class TestPatterns extends JFrame implements ActionListener {
    JTextArea text = new JTextArea(529);
    JTextField pattern = new JTextField(35);
    JButton search = new JButton("Search");
    JButton newSearch = new JButton("New Search");
    JTextArea result = new JTextArea(529);
 
    public TestPatterns() {
        super("Test Patterns");
        setSize(430320);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container pane = getContentPane();
        GridLayout grid = new GridLayout(31);
        pane.setLayout(grid);
        // set up the top row
        JLabel textLabel = new JLabel("Text: ");
        JPanel row1 = new JPanel();
        row1.add(textLabel);
        JScrollPane scroll = new JScrollPane(text,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        row1.add(scroll);
        // set up the middle row
        JPanel row2 = new JPanel();
        JLabel patternLabel = new JLabel("Pattern: ");
        row2.add(patternLabel);
        row2.add(pattern);
        search.addActionListener(this);
        newSearch.addActionListener(this);
        row2.add(search);
        row2.add(newSearch);
        // set up the bottom row
        JPanel row3 = new JPanel();
        JLabel resultLabel = new JLabel("Result: ");
        row3.add(resultLabel);
        result.setEditable(false);
        JScrollPane scroll2 = new JScrollPane(result,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        row3.add(scroll2);
        // set up the content pane
        pane.add(row1);
        pane.add(row2);
        pane.add(row3);
        setContentPane(pane);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if (source == search) {
            checkPattern();
        else {
            pattern.setText("");
            result.setText("");
        }
    }

    private void checkPattern() {
        try {
            if (Pattern.matches(pattern.getText(), text.getText()))
                result.setText("That pattern was found");
            else
                result.setText("That pattern was not found");
        catch (PatternSyntaxException pse) {
            result.setText("Regex error: " + pse.getMessage());
        }
    }

    public static void main(String[] arguments) {
        TestPatterns app = new TestPatterns();
    }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.