java - 使用正则表达式制作 axception word 源代码

标签 java regex

我的程序应该检测到源代码java的方法或构造函数。

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.JOptionPane;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import javax.swing.JScrollPane;

class cek extends JFrame implements ActionListener
{
    JTextArea area=new JTextArea("",5,5);
    JTextArea comment=new JTextArea("",5,5);
    JButton button;
    JScrollPane scrollPane,comment2,kelas2,ori2,tipe2;
    public cek()
    {   
        super("example");
        scrollPane = new JScrollPane(area);
        button=new JButton("button");
        comment.setEditable(false);
        button.addActionListener(this);
        setLocation(0,200);
        setSize(1000,500);
        setLayout(null); 
        scrollPane.setBounds(10,10,467,417);
        comment2=new JScrollPane(comment);
        comment2.setBounds(500,10,450,417);
        button.setBounds(10,430,450,25);
        add(comment2);
        add(button);
        add(scrollPane);
        show();
    }
    public void actionPerformed(ActionEvent e)
    {
        if(button==e.getSource())
        {
            comment.setText("");
            Pattern aaa=Pattern.compile("((public |private |protected )?(String |int |char |void )?([\\w]).*[(].*[)][ ]*([{]|([\\r\\n]*).*[{]))");
            Matcher bbb=aaa.matcher(area.getText());

            int i=0;
            while (bbb.find()) 
            {
                comment.setText(comment.getText() + bbb.group() + "\n");
            }
            Pattern a=Pattern.compile("[{]");
            Matcher b=a.matcher(comment.getText());
            comment.setText(b.replaceAll(""));
        }
    }
    public static void main(String[]args)
    {
        new cek();
    }
}

我的正则表达式

"((public |private |protected )?(String |int |char |void )?([\\w]).*[(].*[)][ ]*([{]|([\\r\\n]*).*[{]))"

但是我的问题为什么像 if、for、while、调用函数也打印出来???

/image/ovBnU.png

在我的正则表达式中,我应该修复什么???

最佳答案

您只需更好地安排捕获组即可。
RegexFormat 这样的程序可以提供很多帮助。

 # "(?s)(public|private|protected)\\s+(?:(static)\\s+)?(?:(String|int|char|void)\\s+)?(\\w.*?)\\((.*?)\\)\\s*.*?{"

 (?s)                     # Modifier dot-all
 (                        # (1 start), Access, required
      public 
   |  private
   |  protected 
 )                        # (1 end)
 \s+ 
 (?:
      ( static )          # (2), Storage, optional
      \s+ 
 )?
 (?:
      (                   # (3 start), Type, optional
           String
        |  int
        |  char
        |  void 
      )                   # (3 end)
      \s+ 
 )?
 ( \w .*? )               # (4), Name, required

 \(                       # '('
 ( .*? )                  # (5), Parameters 
 \)                       # ')'
 \s* .*? {                # '{'

输出:

 **  Grp 0 -  ( pos 551 , len 19 ) 
public cek()
    {  
 **  Grp 1 -  ( pos 551 , len 6 ) 
public  
 **  Grp 2 -  NULL 
 **  Grp 3 -  NULL 
 **  Grp 4 -  ( pos 558 , len 3 ) 
cek  
 **  Grp 5 -  ( pos 562 , len 0 )  EMPTY 

==============================

 **  Grp 0 -  ( pos 1125 , len 49 ) 
public void actionPerformed(ActionEvent e)
    {  
 **  Grp 1 -  ( pos 1125 , len 6 ) 
public  
 **  Grp 2 -  NULL 
 **  Grp 3 -  ( pos 1132 , len 4 ) 
void  
 **  Grp 4 -  ( pos 1137 , len 15 ) 
actionPerformed  
 **  Grp 5 -  ( pos 1153 , len 13 ) 
ActionEvent e  

=============================

 **  Grp 0 -  ( pos 1789 , len 44 ) 
public static void main(String[]args)
    {  
 **  Grp 1 -  ( pos 1789 , len 6 ) 
public  
 **  Grp 2 -  ( pos 1796 , len 6 ) 
static  
 **  Grp 3 -  ( pos 1803 , len 4 ) 
void  
 **  Grp 4 -  ( pos 1808 , len 4 ) 
main  
 **  Grp 5 -  ( pos 1813 , len 12 ) 
String[]args  

关于java - 使用正则表达式制作 axception word 源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26080896/

相关文章:

java - 如何在 JPA 查询中应用忽略大小写

Java地址簿。如何防止代码中出现重复的联系人?

java - 在不知道 Firebase 实时数据库中的键值的情况下检索子值

javascript - 匹配打开和关闭的 html 标签的正则表达式

java - 使用 Java 扫描器时条件未按预期工作

python - 查找重叠的匹配项

c# - Linq2Xml 删除特定属性名称及其值

php - 使用 preg_match 检查字符串是否仅包含 A-Z、a-z 和 -(破折号)字符

C# regex 引用重复命名组

java - 这个 64 位数字变量多线程规则在 Java 1.8 中仍然适用还是已经过时了?