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
, 어떻게 해결해야한다 ???
당신은'사용해야 *'주의.. – Makoto
소스 코드를 분석하기 위해 정규식을 사용하는 것은 거의 항상 나쁜 생각입니다. 당신이 틀리게 될 것 같은 애매한 (또는 그렇게 애매한) 경우가 거의 불가피합니다. 예를 들어, 주석이나 문자열, 또는 유니 코드를 사용하는 사람들이 이상하고 모호한 방식으로 도망칩니다. –
내 대답은 * "무엇을 해결해야합니까?"* 분석 코드를 버리거나 기존의 Java 파서/소스 코드 분석기 라이브러리를 찾고이를 기반으로 분석기를 구현하는 것입니다. (또는 재미있는 일을하고 있다면 Javacc 또는 Antlr을 사용하여 파서를 처음부터 개발하십시오.) –