gpt4 book ai didi

java,正则表达式字符串不包含/之间的一组单词

转载 作者:行者123 更新时间:2023-12-01 23:26:01 24 4
gpt4 key购买 nike

我知道这似乎是一个常见问题,但尽管进行搜索仍无法解决我的问题。我需要一个正则表达式,仅匹配不以指定单词集开头且由/包围的字符串。示例:

/harry/white
/sebastian/red
/tom/black
/tomas/green

我不想要以/harry/和/tom/开头的字符串,所以我希望

/harry/white     NO
/sebastian/red YES
/tom/black NO
/tomas/green YES

1) ^/(?!(harry|tom)).* doesn't match /tomas/green
2) ^/(?!(harry|tom))/.* matchs nothing
3) ^/((harry|tom))/.* matchs the opposite

什么是正确的正则表达式?如果有人解释一下为什么 1 和 2 是错误的,我将不胜感激。请不要责怪我:)谢谢。

最佳答案

您需要在否定前瞻内(而不是外部)为它们添加结尾斜杠:

^/(?!(harry|tom)/).*

不添加斜杠,将匹配 tomas 中的 tom,并且否定的前瞻不会满足。

关于java,正则表达式字符串不包含/之间的一组单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19960619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com