Ian Davis
Ian Davis

Reputation: 19413

Simple regex not working in c#

/news/article-title.html

is not being caught by the regex:

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

?

I'm trying to have articles that do NOT have "archives" in the filename, but start with "/news/"

Thanks!

Upvotes: 1

Views: 111

Answers (2)

Kobi
Kobi

Reputation: 137997

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-).+
  • make sure \.html is at the end (it may appear more than once): \.html(?=$|[?&])

Upvotes: 6

unholysampler
unholysampler

Reputation: 17321

You can't use the not of a character block to not an entire string.

[^(archives)]

This is interpreted as a character that is not one of the following: (, a, r, c, h, i, v, e, s or ).

Upvotes: 1

Related Questions