-5

I need a Regex for a set including only files from /src/main/java/{any_package}/*.java to apply CheckStyle rules only to these files in Eclipse.

All other files, e.g.: none *.java files, src/test/ should be ignored.

blekione
  • 10,152
  • 3
  • 20
  • 26

2 Answers2

1

Maybe, this expression would also function here, even though your original expression is OK.

^\/src\/main\/java(\/[^\/]+)?\/\*\.java$

Demo 1

My guess is that here we wish to pass:

/src/main/java/{any_package}/*.java
/src/main/java/*.java

If the second one is undesired, then we would simply remove the optional group:

^\/src\/main\/java(\/[^\/]+)\/\*\.java$

Demo 2

and it might still work.

Test

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "^\\/src\\/main\\/java(\\/[^\\/]+)\\/\\*\\.java$";
final String string = "/src/main/java/{any_package}/*.java\n"
     + "/src/main/java/*.java";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Emma
  • 27,428
  • 11
  • 44
  • 69
-1

The regex I'm looking for is src[\/]main\/java\/?(?:[^\/]+\/?)*.java$

Regex 101 test demo

blekione
  • 10,152
  • 3
  • 20
  • 26
  • This question and self-answer is of no use. It is just one example of gazillions of examples of how to write a regex to match a pattern. Do you really think StackOverflow should be filled with examples of every pattern people can dream up? That's not useful. – Andreas May 15 '19 at 18:03
  • Fair point. I have written it more as a reference for myself. I'm not a regex guru and today it was not a first time I was asking the Internet for exactly this regex, without success. Next time I will look for it (it will happen sooner or later) I will know where to look for. Or maybe it is time to create private wiki? – blekione May 15 '19 at 20:39
  • Private wiki sounds like the right place to store homegrown code snippets for re-use. You can even make it a (semi-)public wiki, if you want to share your snippets, but StackOverflow is not for that. – Andreas May 15 '19 at 21:48