Mark Handy
Mark Handy

Reputation: 1256

RegEx to support letters only, in two groups separated by a period, with a restriction on the second group

I'm trying to write a url rewrite regex expression. So if the url is bob.smith then good, if it's home.aspx, then not.

I have this so far:

^[a-zA-Z\-]+[.](?!aspx$|js$|css$|html$|htm$)[a-zA-Z\-]+$

So, first group, upper or lower case letters, the period, then the second group, upper or lower case letters, but not aspx, js, cs, html, htm.

Am i on the right track here?

Upvotes: 1

Views: 51

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626952

Use

^[a-zA-Z-]+\.(?!(?:aspx|js|css|html?)$)[a-zA-Z-]+$

See the regex demo and the regex graph:

enter image description here

Details

  • ^ - start of string
  • [a-zA-Z-]+ - 1 or more letters or hyphens
  • \. - a dot -(?!(?:aspx|js|css|html?)$) - after the dot, there should not be aspx, js, css, htm or html at the end of the string
  • [a-zA-Z-]+ - 1+ letters or hyphens
  • $ - end of string.

Upvotes: 1

Related Questions