Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex inverse matching on specific string?

I would like to match the following

  • com.my.company.moduleA.MyClassName
  • com.my.company.moduleB.MyClassName
  • com.my.company.anythingElse.MyClassName

but not the following

  • com.my.company.core.MyClassName

My current simple regex pattern is :

Pattern PATTERN_MODULE_NAME = Pattern.compile("com\\.my\\.company\\.(.*?)\\..*")

Matcher matcher = PATTERN_MODULE_NAME.matcher(className);
if (matcher.matches()) {
    // will return the string inside the parentheses (.*?)
    return matcher.group(1);
}

So, basically, how can i match everything else, but not a specific string, which is the string core in my case.

Please share your ideas on how to achieve that in Java ?

Thank you !

like image 845
Albert Gan Avatar asked Dec 21 '22 13:12

Albert Gan


1 Answers

You can use the following regex:

^com\\.my\\.company\\.(?!core).+?\\.MyClassName$
like image 117
codaddict Avatar answered Jan 05 '23 03:01

codaddict