Search code examples
regexnsregularexpression

Regex to match word that contains exact word


I'm trying to filter specific files in Java by their names using Regex.

Idea being a lot of files are called SomethingSupport.java, AnotherSupport.java, MoreThingsSupport.java, so as they all have the "Support.java" I was trying to do:

[Support.java]

But of course that's meant for characters so it will filter S,u,p,o, etc... Looking through RegExr I've tried:

(Support.java)

But it takes all "Support.java" occurrences but I'm trying to take ThingsSupport.java, SomethingSupport.java, etc. not Support.java.


Solution

  • Parentheses just group things. There is no difference between what regex "Support.java" and regex "(Support.java)" would match.

    Note that . is regexpese for any character, so, e.g. Supportxjava, unlikely as it is that you have a file with that name in your source base, would match too. \. is regexpese for "an actual dot, please".

    I think you're looking for the regex .*Support.*\.java. Which means: Absolutely anything (0 or more any character), followed by the string Support, followed by absolutely anything again, followed by .java.

    That would find e.g. FooSupportBar.java, Support.java, HelloSupport.java, and SupportHello.java. It wouldn't find anything that doesn't end in .java.