【发布时间】:2021-05-19 10:25:58
【问题描述】:
我想匹配来自 url 的搜索短语中的 3 个单词的正则表达式,但不匹配 4 个或更多。 URL 可以有一些变化。问题如下图所示。正则表达式应该匹配和不匹配以下示例:
SHOULD MATCH:
https://example.com/search=any%20url%20encoded_word-here
https://example.com/search=any%20url%20encoded_word-here%20
https://example.com/search=z%C5%82oty%20z%C5%82oty%20z%C5%82oty
https://example.com/search=z%C5%82oty%20z%C5%82ota%20%C5%82ata
https://example.com/search=any%20%20word%20%20here
https://example.com/search=any%20word%20here&color=blue
https://example.com/search=any-1st%20word_2nd%20here3
SHOULD NOT MATCH:
https://example.com/search=one%20two%20three%20four
https://example.com/search=one%20%20two%20%20three%20%20four
https://example.com/search=one%20%20two%20three%20%20four
https://example.com/search=one%20%20two%20%20three%20%20four
https://example.com/search=one%20two%20three%20four&color=blue
https://example.com/search=z%C5%82oty%20z%C5%82oty%20z%C5%82oty%20word
从https://regex101.com/r/0qzCJV/1 开始,但我不知道如何在条件上不匹配。大家能帮帮我吗?
【问题讨论】:
-
是什么决定了
URL中的单词边界?换句话说,搜索中单词之间的分隔符是什么?一次我看到%20和一次& -
类似
.*/search=(?:(?:(?!%20).)*(?:%20|$)+){3}$? -
或者,
.*/search=((?:(?!%20).)*)(?:%20)+((?:(?!%20).)*)(?:%20)+((?:(?!%20).)*)(?:%20)*$(demo)? -
单词边界是空格 (%20) - 感谢它现在可以使用 - 救了我的命 :)
标签: regex regex-negation