我有一个用例,我正在字符串中搜索特定的子字符串,如果该特定字符串包含另一个特定的子字符串,我希望它被拒绝。
前任:
pikachu_is_the_best_ever_in_the_world_go_pikachumew_is_the_best_ever_in_the_world_go_mewraichu_is_the_best_ever_in_the_world_go_raichu我希望我的正则表达式能够选取包含单词“best”而不是单词“mew”的字符串,即第一个和第三个字符串。
我尝试将^(.*best).*$和组合^((?!mew).)*$到下面的表达式中,第二个正则表达式仅忽略字符串开头存在“mew”的单词。
^(.*best)((?!mew).).*$
Run Code Online (Sandbox Code Playgroud)
并且已经尝试过
^((?!mew).)(.*best).*$
Run Code Online (Sandbox Code Playgroud)
^(?=.*best)(?:(?!mew).)*$. matches newline解释:
^ : start of line
(?= : positive lookahead
.* : 0 or more any character but newline
best : literally "best"
) : end lookahead
(?: : start non capture group
(?! : negative lookahead, make sure we don't have
mew : literally "mew"
) : end lookahead
. : any character but newline
)* : group may appear 0 or more times
$ : end of line
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35651 次 |
| 最近记录: |