Skip to main content
 首页 » 编程设计

python之需要正则表达式帮助 : Match if certain string doesn't start with certain sub-patterns AND certain character should not be there

2025年05月04日32EasonJim

我需要为一个小项目想出一个正则表达式。

字符串不应以:

"/wiki" 

而且它也不应该有下面的模式 "/.*:.*"(基本上模式以字符 '/' 开头,之后出现任何 ':')

而且它也不能有某个字符'#'

所以基本上所有这些字符串都会失败:

"/wiki/index.php?title=ROM/TAP&action=edit&section=2" 
"/User:romamns" 
"/Special:Watchlist" 
"/Space_Wiki:Privacy_policy" 
"#column-one" 

所有这些字符串都会通过:

"/ROM/TAP/mouse" 
"http://www.boost.org/" 

我将在 python 中使用正则表达式(如果有任何不同的话)。

感谢您的帮助。

请您参考如下方法:

^(/(?!wiki)[^:#]*|[^#/][^#]*)$ 应该没问题,经过测试here ,当然我可能会遗漏一些东西,但这似乎符合您的规范。