0

I want to find methods and parametres names with RegEx in Java. I have a C file and I read this file. My Regex part can find method of names but additional, it finds other names such as printf, scanf... I need just names of methods.

public class Deneme {


public static void main(String[] args) throws FileNotFoundException, IOException {

    try{
        File f=new File("C:\\Users\\Emre\\Documents\\NetBeansProjects\\deneme\\src\\deneme\\Program.c"); //Open a file
        String s;
        FileReader reader=new FileReader(f); 
        BufferedReader br=new BufferedReader(reader); //Read file
        while((s=br.readLine())!=null){
            String regex="(?!\\bif\\b|\\bfor\\b|\\bwhile\\b|\\bswitch\\b|\\btry\\b|\\bcatch\\b)(\\b[\\w]+\\b)[\\s\\n\\r]*(?=\\(.*\\))";
            Pattern funcPattern = Pattern.compile(regex);
            Matcher m = funcPattern.matcher(s); //Matcher is used to match pattern with string 
            if(m.find()){
                System.out.println(m.group(0));
            }
        }
    }


    catch(Exception e){
            e.printStackTrace();
    }
}

}

This is my C file

#include "stdio.h" 
#include "stdlib.h" 

void DiziYazdir(int *p,int uzunluk)
{   
int i=0;  
for(;i<uzunluk;i++) 
printf("%d ",p[i]); 
} 

int DiziTopla(int *p,int uzunluk)
{ 
int i=0;  int toplam=0;  
for(;i<uzunluk;i++) 
toplam += p[i];  
return toplam; 
}

int main()
{ 
int x,y;  
printf("x:");  
scanf("%d",&x);  
printf("y:");  
scanf("%d",&y);  
int sonuc = x + y;  
printf("Sonuc:%d\n\n",sonuc);  
int *dizi = malloc(3*sizeof(int));  
dizi[0]=x;  
dizi[1]=y;  
dizi[2]=sonuc;  
DiziYazdir(dizi,3);  
printf("\n\nToplam Deger:%d",DiziTopla(dizi,3));  
free(dizi);  
return 0; 
}

There are lots of methods and I cannot write all the methods in "String regex" like "\bfor\b|\bwhile\b". Is there any RegEx method to fix that ? or Is there a RegEx method that covers the whole? It will just print names of method and parameters such as DiziYazdir, DiziTopla, Main and for methods *p, int uzunluk.

1 Answer 1

1

The following reg ex should work fine, one word starting the line followed by space and another word (letters and digits) and then any character sequence inside a pair of parantheses followed by possibly a space and then a {

^\w*\s+[\w\d]+\(.*\)\s?\{?

as a java string I guess it should be

String pattern = "^\\w*\\s+[\\w\\d]+\\(.*\\)\s?\\{?";
Sign up to request clarification or add additional context in comments.

2 Comments

I tried but I didn't show anything. Thank you for your comment
@EmreOzuslu I tried my pattern in a text editor which mean it could match past end-of-line but this isn't possible here since you read one line at a time so the match of the bracket at end needs to be optional. I update my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.