匹配字符串而不是单词的正则表达式

时间:2022-06-05 04:04:51

I'd like to see what internal libraries are used in my Java project by searching through the code for

我想通过搜索Java项目的代码来查看在我的Java项目中使用了哪些内部库

import com.mycompany.someproject.path.ClassName;

Let's say that my project's title is 'myproject'. What regex would match all lines that begin with

假设我的项目的标题是“myproject”。regex将匹配所有以开头的行

import com.mycompany.

and exclude:

排除:

myproject.path...

Matched lines would be:

匹配的线路是:

import com.mycompany.tool.path.SomeClass;
import com.mycompany.sallysproject.path.SomeOtherClass;

and exclude all internal project imports:

并排除所有国内项目进口:

import com.mycompany.myproject.*

1 个解决方案

#1


3  

This should work:

这应该工作:

import com\.mycompany\.(?!myproject\.).*

Explanation:

解释:

import com\.mycompany\. - The line must start with import com.mycompany.. Pretty self-explanatory; note that we need to escape the periods -- \. -- so that they actually match periods, and not "any character".

com \ .mycompany \进口。-这条线必须从进口公司开始。非常容易理解;注意,我们需要避开周期——\。——这样它们实际上就匹配了句点,而不是“任何字符”。

(?!myproject\.) - This is called a "negative lookahead". The overall match will succeed only if the pattern inside the parentheses (except for ?!) does not match.

(?!我的项目)-这被称为“负面展望”。只有当括号内的模式(除了?!)不匹配时,全局匹配才会成功。

.* - Anything after import com.mycompany. (except for myproject.) will be matched.

进口公司以后的任何事情。(除了myproject.)将会匹配。

#1


3  

This should work:

这应该工作:

import com\.mycompany\.(?!myproject\.).*

Explanation:

解释:

import com\.mycompany\. - The line must start with import com.mycompany.. Pretty self-explanatory; note that we need to escape the periods -- \. -- so that they actually match periods, and not "any character".

com \ .mycompany \进口。-这条线必须从进口公司开始。非常容易理解;注意,我们需要避开周期——\。——这样它们实际上就匹配了句点,而不是“任何字符”。

(?!myproject\.) - This is called a "negative lookahead". The overall match will succeed only if the pattern inside the parentheses (except for ?!) does not match.

(?!我的项目)-这被称为“负面展望”。只有当括号内的模式(除了?!)不匹配时,全局匹配才会成功。

.* - Anything after import com.mycompany. (except for myproject.) will be matched.

进口公司以后的任何事情。(除了myproject.)将会匹配。