0

I'm working on a project that involves reading in a .java file and reporting on aspects of the code, such as functions, line counts and indentation. I would like to use a function declaration as a delimiter when scanning the file. To identify when functions are called and conditionals are used, I've successfully constructed this pattern. The pattern starts with any sequence of characters, followed by a pair of parentheses which may or may not contain text, followed by an opening brace.

scanner.useDelimiter(".*(\\s*)?\\x28.*\\x29(\\s*)?(\\n)?\\x7B");

This matches on function declarations such as:

foo(int n, String w) {
foo_bar() {

or conditionals such as...

while(foo == null) {
if(i < 10) {

etc.

What I want to do now is to exclude all conditional statements or any other statement that uses this same pattern: i.e. anything that matches this pattern but excludes if, while, for, catch, etc. The idea is it will use function declarations as its delimiter and nothing else.

I have tried to negate the terms from the pattern...

scanner.useDelimiter("(!?.*(if|while|for|switch|catch))
(\\s*)\\x28.*\\x29(\\s*)?(\\n)?\\x7B");

... which doesn't negate the conditionals.

And I've tried negative lookbehind.

scanner.useDelimiter("?<!if|while|for|switch|catch)(\\s*)?\\x28.*\\x29(\\s*)?
(\\n)?\\x7B");

... which just messes it all up.

It could well be that I don't fully understand regular expressions yet. Any tips?

3
  • Don't you mean 'function declaration', rather than 'function call'? Commented Jul 7, 2017 at 13:14
  • Possible duplicate of Regex that Will Match a Java Method Declaration Commented Jul 7, 2017 at 13:23
  • Yes, have corrected it. Thanks for pointing that out Commented Jul 7, 2017 at 13:28

0

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.