简体   繁体   English

简单的正则表达式在C#中不起作用

[英]Simple regex not working in c#

/news/article-title.html /news/article-title.html

is not being caught by the regex: 不被正则表达式捕获:

^/news/[^(archives)].+.html ^ / news / [^(archives)]。+。html

?

I'm trying to have articles that do NOT have "archives" in the filename, but start with "/news/" 我正在尝试撰写文件名中没有“档案”的文章,但以“ / news /”开头

Thanks! 谢谢!

You should use a negative lookahead . 您应该使用否定的前瞻 Character classes only work for a single character. 字符类仅适用于单个字符。 Also, don't forget to escape the dot. 另外,别忘了逃脱圆点。

If "archives" cannot be at the beginning: 如果“档案”不能放在开头:

^/news/(?!archives).+\.html

If "archives" cannot anywhere: 如果“档案”无法在任何地方:

^/news/((?!archives).)+\.html

More tips: 更多提示:

  • Disallow archives as a whole word: (?!archives\\b).+ or (?!archives-).+ 禁止将archives整体视为:( (?!archives\\b).+(?!archives-).+
  • make sure \\.html is at the end (it may appear more than once): \\.html(?=$|[?&]) 确保\\.html位于末尾(它可能出现多次): \\.html(?=$|[?&])

You can't use the not of a character block to not an entire string. 您不能将not of character块用于整个字符串。

[^(archives)]

This is interpreted as a character that is not one of the following: (, a, r, c, h, i, v, e, s or ). 这被解释为不是以下字符之一的字符:(,a,r,c,h,i,v,e,s或)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM