如何创建一个正则表达式来匹配包含一个列表中的单词且不包含另一个列表中的单词的字符串?
How can I create a regex that matches a string contains a word from one list AND does not contains word from another list?
如何创建一个正则表达式来匹配包含一个列表中的单词但不包含另一个列表中的单词的字符串?
我想创建一个正则表达式来匹配包含以下任何单词 (fox,cat) 但不包含 (red,black) 的字符串。
例如:
The quick brown fox jumps over the lazy dog --- match
The quick red fox jumps over the lazy dog ---- no match
The quick brown lion jumps over the lazy cat---- match
The quick black cat jumps over the lazy dog ---- no match
正则表达式可以吗?
试试正则表达式:^(?:(?!(red|black)).)*(?:fox|cat)(?:(?!(red|black)).)*$
解释:
Non-capturing group (?:(?!(red|black)).)* - Confirms red or black is not present before fox or cat
Negative Lookahead (?!(red|black)) - Assert that the Regex below does not match
1st Capturing Group (red|black)
1st Alternative red - red matches the characters red literally (case sensitive)
2nd Alternative black - black matches the characters black literally (case sensitive)
. matches any character (except for line terminators)
Non-capturing group (?:fox|cat) - confirms fox or cat is present
1st Alternative fox - fox matches the characters fox literally (case sensitive)
2nd Alternative cat - cat matches the characters cat literally (case sensitive)
Non-capturing group (?:(?!(red|black)).)* - Confirms red or black is not present after fox or cat
Negative Lookahead (?!(red|black)) - Assert that the Regex below does not match
1st Capturing Group (red|black)
1st Alternative red - red matches the characters red literally (case sensitive)
2nd Alternative black - black matches the characters black literally (case sensitive)
. matches any character (except for line terminators)
如何创建一个正则表达式来匹配包含一个列表中的单词但不包含另一个列表中的单词的字符串?
我想创建一个正则表达式来匹配包含以下任何单词 (fox,cat) 但不包含 (red,black) 的字符串。
例如:
The quick brown fox jumps over the lazy dog --- match
The quick red fox jumps over the lazy dog ---- no match
The quick brown lion jumps over the lazy cat---- match
The quick black cat jumps over the lazy dog ---- no match
正则表达式可以吗?
试试正则表达式:^(?:(?!(red|black)).)*(?:fox|cat)(?:(?!(red|black)).)*$
解释:
Non-capturing group (?:(?!(red|black)).)* - Confirms red or black is not present before fox or cat
Negative Lookahead (?!(red|black)) - Assert that the Regex below does not match
1st Capturing Group (red|black)
1st Alternative red - red matches the characters red literally (case sensitive)
2nd Alternative black - black matches the characters black literally (case sensitive)
. matches any character (except for line terminators)
Non-capturing group (?:fox|cat) - confirms fox or cat is present
1st Alternative fox - fox matches the characters fox literally (case sensitive)
2nd Alternative cat - cat matches the characters cat literally (case sensitive)
Non-capturing group (?:(?!(red|black)).)* - Confirms red or black is not present after fox or cat
Negative Lookahead (?!(red|black)) - Assert that the Regex below does not match
1st Capturing Group (red|black)
1st Alternative red - red matches the characters red literally (case sensitive)
2nd Alternative black - black matches the characters black literally (case sensitive)
. matches any character (except for line terminators)