2014-09-28 2 views
1

내 프로그램이 소스 코드 java로 메소드 또는 생성자를 감지해야합니다.정규식을 사용하여 axulation word 소스 코드를 작성하십시오

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]*).*[{]))" 

하지만 내 문제는 왜, 경우처럼 대한 반면, 기능이 너무 인쇄 부르심 ??? 내 정규식에서

http://i.stack.imgur.com/ovBnU.png

, 어떻게 해결해야한다 ???

+1

당신은'사용해야 *'주의.. – Makoto

+0

소스 코드를 분석하기 위해 정규식을 사용하는 것은 거의 항상 나쁜 생각입니다. 당신이 틀리게 될 것 같은 애매한 (또는 그렇게 애매한) 경우가 거의 불가피합니다. 예를 들어, 주석이나 문자열, 또는 유니 코드를 사용하는 사람들이 이상하고 모호한 방식으로 도망칩니다. –

+1

내 대답은 * "무엇을 해결해야합니까?"* 분석 코드를 버리거나 기존의 Java 파서/소스 코드 분석기 라이브러리를 찾고이를 기반으로 분석기를 구현하는 것입니다. (또는 재미있는 일을하고 있다면 Javacc 또는 Antlr을 사용하여 파서를 처음부터 개발하십시오.) –

답변

0

캡쳐 그룹을 조금만 조정하면됩니다.
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 
관련 문제