这是我很久以前在网上找到的一些旧代码。
在顶部文本区域粘贴一些代码。在文本字段中键入正则表达式。您可以在键入或单击按钮时进行搜索,具体取决于复选框的状态。
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class RegexKing implements ActionListener, DocumentListener
{
public static void main(String[] args)
{
new RegexKing();
}
public RegexKing()
{
initGUI();
setupGUI();
displayFrame();
}
public void initGUI()
{
// init
frame = new JFrame("Regex King");
container = new JPanel();
inputArea = new JTextArea();
regexField =new JTextField();
outputArea = new JTextArea();
quickMatch = new JCheckBox("Attempt Match on Change");
matchButton = new JButton("Match");
inputScroll = new JScrollPane(inputArea);
outputScroll = new JScrollPane(outputArea);
// setup
outputArea.setEditable(false);
matchButton.addActionListener(this);
regexField.getDocument().addDocumentListener(this);
// key binding
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, Event.SHIFT_MASK, false);
Action keyact = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
doMatch();
}
};
container.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(key, "DO_MATCH");
container.getActionMap().put("DO_MATCH", keyact);
}
public void setupGUI()
{
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
container.setLayout(gbl);
setInsets(2, 2, 2, 2);
gbcf = GridBagConstraints.BOTH;
setComp(inputScroll, 10, 10, 20, 1, 1, 1);
setComp(quickMatch, 10, 20, 1, 1, 1, .1);
setComp(matchButton, 20, 20, 1, 1, 1, .1);
setComp(regexField, 10, 30, 20, 1, 1, .1);
setComp(outputScroll, 10, 40, 20, 1, 1, 1);
}
public void setComp(JComponent comp, int gx, int gy, int gw, int gh, double wx, double wy)
{
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
gbc.fill = gbcf;
gbl.setConstraints(comp, gbc);
container.add(comp);
}
public void setInsets(int top, int bottom, int left, int right)
{
gbc.insets.top = top;
gbc.insets.bottom = bottom;
gbc.insets.left = left;
gbc.insets.right = right;
}
public void displayFrame()
{
frame.setContentPane(container);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void insertUpdate(DocumentEvent e)
{
if(quickMatch.isSelected() == true)
{
doMatch();
}
}
public void removeUpdate(DocumentEvent e)
{
if(quickMatch.isSelected() == true)
{
doMatch();
}
}
public void changedUpdate(DocumentEvent e) {}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource() == matchButton)
{
doMatch();
}
}
public void doMatch()
{
outputArea.setText("\nAttempting Match...");
inputArea.getHighlighter().removeAllHighlights();
try
{
String inputText = inputArea.getText();
Pattern pattern = Pattern.compile(regexField.getText());
Matcher matcher = pattern.matcher(inputText);
int matchCount = 0;
while(matcher.find())
{
matchCount++;
outputArea.append("\n\nMatch #" + matchCount);
for (int i = 0; i < matcher.groupCount() + 1; i++)
{
outputArea.append("\nGroup #" + i + ": " + matcher.group(i));
outputArea.append(" , " + matcher.end(i));
int start = matcher.start(i);
int end = matcher.end(i);
System.out.println(start + " : " + end);
inputArea.getHighlighter().addHighlight( start, end, DefaultHighlighter.DefaultPainter );
System.out.println(i);
if (matchCount == 1) inputArea.setCaretPosition(start);
}
}
}
catch(Exception exc)
{
outputArea.append("\nEXCEPTION THROWN");
}
outputArea.append("\n\nFinished.\n");
}
GridBagLayout gbl;
GridBagConstraints gbc;
int gbcf;
JFrame frame;
JPanel container;
JTextArea inputArea;
JTextField regexField;
JTextArea outputArea;
JScrollPane inputScroll;
JScrollPane outputScroll;
JCheckBox quickMatch;
JButton matchButton;
}