本文介绍了特定字符串上的正则表达式反向匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想匹配以下


  • com.my.company。 moduleA .MyClassName

  • com.my.company。 moduleB .MyClassName

  • com.my.company。 anythingElse .MyClassName

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

以下

but not the following


  • com.my.company。核心 .MyClassName

  • 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.

请分享您对如何在Java中实现这一点的想法?

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

谢谢!

推荐答案

您可以使用以下正则表达式:

You can use the following regex:

^com\\.my\\.company\\.(?!core).+?\\.MyClassName$

这篇关于特定字符串上的正则表达式反向匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 00:27