자바 테스트 정규 표현 식 도구

4473 단어 정규 표현 식
import javax.swing.*;  
import javax.swing.text.DefaultHighlighter;  
import javax.swing.text.BadLocationException;  
import java.awt.BorderLayout;  
import java.awt.Font;  
import java.awt.HeadlessException;  
import java.awt.Color;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.util.regex.Pattern;  
import java.util.regex.Matcher;  
   
/** 
 *    Java       
 */  
public class RegTester {  
   
    public static void main(String[] args) {  
        try {  
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
   
        new RegTesterFrame().setVisible(true);  
    }  
}  
   
class RegTesterFrame extends JFrame {  
   
    private JTextField textbox = new JTextField();  //             
       
    private JTextArea textarea = new JTextArea();   //              
   
    private RegTesterFrame thisFrame = this;  
       
    private DefaultHighlighter.DefaultHighlightPainter highlightPainter =  
            new DefaultHighlighter.DefaultHighlightPainter(Color.green);  
   
    //       
    RegTesterFrame() throws HeadlessException {  
        super("   Java      ");  
        setSize(500, 300);  
        setLocation(300, 100);  
        setDefaultCloseOperation(EXIT_ON_CLOSE);  
   
        setupComponents();  
    }  
   
    //        
    private void setupComponents() {  
        setupBorder();  
   
        JPanel topPanel = new JPanel(new BorderLayout(5, 0));  
        this.add(topPanel, BorderLayout.NORTH);  
        setupTopPanel(topPanel);  
   
        JPanel centerPanel = new JPanel(new BorderLayout());  
        this.add(centerPanel, BorderLayout.CENTER);  
        setupCenterPanel(centerPanel);  
    }  
   
    //            
    private void setupBorder() {  
        JPanel panel = new JPanel(new BorderLayout(5, 5));  
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));  
        this.setContentPane(panel);  
    }  
   
    //        
    private void setupCenterPanel(JPanel panel) {  
        textarea.setFont(new Font(Font.DIALOG_INPUT, 0, 12));  
        panel.add(new JScrollPane(textarea));  
    }  
   
    //       
    private void setupTopPanel(JPanel panel) {  
        textbox.setFont(new Font(Font.DIALOG_INPUT, 0, 12));  
        panel.add(textbox, BorderLayout.CENTER);  
   
        JButton button = new JButton("  ");  
        button.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
                checkRegularExpression();  
            }  
        });  
        panel.add(button, BorderLayout.EAST);  
        panel.add(new JLabel("     :"), BorderLayout.WEST);  
    }  
   
    //          
    private void checkRegularExpression() {  
        textarea.getHighlighter().removeAllHighlights();  
   
        String reg = textbox.getText();  
        String text = textarea.getText();  
   
        Pattern p = Pattern.compile(reg);  
        Matcher m = p.matcher(text);  
        if (m.find()) {  
            thisFrame.setTitle("       。");  
            highlightMatches(m);  
        } else {  
            thisFrame.setTitle("        。");  
        }  
    }  
   
    //                  
    private void highlightMatches(Matcher m) {  
        highlight(m);  
        int start = m.end();  
        while (m.find(start)) {  
            highlight(m);  
            start = m.end();  
        }  
    }  
   
    private void highlight(Matcher m) {  
        try {  
            textarea.getHighlighter().addHighlight(m.start(), m.end(), highlightPainter);  
        } catch (BadLocationException e) {  
            e.printStackTrace();  
        }  
    }  
} 
 

좋은 웹페이지 즐겨찾기