括号中的文本的正则表达式,不包含特定单词

时间:2022-05-12 06:34:58

I found some expressions like

我发现了一些表达方式

\((.*?)\) 

which works good to find any text in brackets with brackets like (text)

这样可以很好地找到括号中的任何文本,如(文本)

and

^((?!.*(word1|word2|word3)).*)+

to find text which contains no one specific word like word4abcd and not for example word1 test

查找不包含word4abcd等特定单词的文本,而不是word1 test

How to merge them to find text in brackets which not contains these words like in (example) and not like (example word2)?

如何合并它们以在括号中查找不包含这些单词的文本,例如(示例)而不是(例如word2)?

Thanks in advance

提前致谢

3 个解决方案

#1


1  

The first regular expression uses a reluctant quantifier *? to make sure that the first available closing bracket is matched after the opening bracket. The second regular expression uses a zero-width negative look-ahead group (that's the (?!...) construction) to prevent matching certain words. To combine these tricks we're looking at something like this:

第一个正则表达式使用了一个不情愿的量词*?确保第一个可用的关闭支架在开口支架后匹配。第二个正则表达式使用零宽度负前瞻组(即(?!...)构造)来防止匹配某些单词。要结合这些技巧我们正在寻找这样的事情:

\(((?!something).*?)\)

The question is what goes in the place of the something. Simply putting .*(word1|word2) there will not work: this will also forbid word1 or word2 outside the brackets. Replacing .* by .*? does not change that. What does work is [^)]*(word1|word2) which will match any sequence of characters unequal to ) followed by word1 or word2.

问题是什么代替了什么。简单地放。*(word1 | word2)就行不通了:这也禁止括号外的word1或word2。更换。* by。*?不会改变这一点。什么工作是[^)] *(word1 | word2),它将匹配任何不等于的字符序列,后跟word1或word2。

The resultant expression then is

结果表达式就是

\(((?![^)]*(word1|word2)).*?)\)

which will match a bracketed expression that does not contain word1 or word2.

这将匹配不包含word1或word2的括号表达式。

#2


0  

Question isn't very clear but I am giving an example that might help you.

问题不是很明确,但我举一个可能对你有帮助的例子。

Look at this regex:

看看这个正则表达式:

/\((?!.*?\bexample\s+word\b[^)]*\)).*?\bexample\b.*?\)/

This matches word example inside square brackets. match will happen if there is no example word in the square brackets.

这匹配方括号内的单词示例。如果方括号中没有示例单词,则会发生匹配。

\b has been used for word boundaries so that examples is not matched instead.

\ b已用于字边界,因此不匹配示例。

#3


0  

Maybe this it what you want?

也许这就是你想要的?

^\(((?!.*word1)(\S)*)|(\3)\)

Match: (wordwordexample) Not Match: (word wordexample) (word1wordexample)

匹配:(wordwordexample)不匹配:( word wordexample)(word1wordexample)

#1


1  

The first regular expression uses a reluctant quantifier *? to make sure that the first available closing bracket is matched after the opening bracket. The second regular expression uses a zero-width negative look-ahead group (that's the (?!...) construction) to prevent matching certain words. To combine these tricks we're looking at something like this:

第一个正则表达式使用了一个不情愿的量词*?确保第一个可用的关闭支架在开口支架后匹配。第二个正则表达式使用零宽度负前瞻组(即(?!...)构造)来防止匹配某些单词。要结合这些技巧我们正在寻找这样的事情:

\(((?!something).*?)\)

The question is what goes in the place of the something. Simply putting .*(word1|word2) there will not work: this will also forbid word1 or word2 outside the brackets. Replacing .* by .*? does not change that. What does work is [^)]*(word1|word2) which will match any sequence of characters unequal to ) followed by word1 or word2.

问题是什么代替了什么。简单地放。*(word1 | word2)就行不通了:这也禁止括号外的word1或word2。更换。* by。*?不会改变这一点。什么工作是[^)] *(word1 | word2),它将匹配任何不等于的字符序列,后跟word1或word2。

The resultant expression then is

结果表达式就是

\(((?![^)]*(word1|word2)).*?)\)

which will match a bracketed expression that does not contain word1 or word2.

这将匹配不包含word1或word2的括号表达式。

#2


0  

Question isn't very clear but I am giving an example that might help you.

问题不是很明确,但我举一个可能对你有帮助的例子。

Look at this regex:

看看这个正则表达式:

/\((?!.*?\bexample\s+word\b[^)]*\)).*?\bexample\b.*?\)/

This matches word example inside square brackets. match will happen if there is no example word in the square brackets.

这匹配方括号内的单词示例。如果方括号中没有示例单词,则会发生匹配。

\b has been used for word boundaries so that examples is not matched instead.

\ b已用于字边界,因此不匹配示例。

#3


0  

Maybe this it what you want?

也许这就是你想要的?

^\(((?!.*word1)(\S)*)|(\3)\)

Match: (wordwordexample) Not Match: (word wordexample) (word1wordexample)

匹配:(wordwordexample)不匹配:( word wordexample)(word1wordexample)