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?

0 Answers0