简体   繁体   中英

Regex to exclude 8 digit number

I have the following URL schema:

/test/how-are-you.html
/test/hello-how-are-you.html
/test/im-fine.html
/test/im-fine/how-are-you/
/test/im-fine/hello-how-are-you/
/test/happy-1-day.html
/test/thanks-123.html
/test/thanks-1234.html
/test/thanks-12345.html
/test/hoe-are-you-10012396.html (always 8 numbers)
/test/im-fine-10012396.html (always 8 numbers)
/test/hello-52345786.html (always 8 numbers)

Can you give me a regex to exclude the last 3 examples, while including the rest? I tried something like

^\/test\/?[\w\-]*\/?[\w\-]*([^0-9]{8})\/?

and failed ;)

Best, Daniel

You may use

^\/test\/(?!.*\d{8}\.html$).*$

See the regex demo

Details

  • ^ - start of string
  • \\/test\\/ - /test/ string
  • (?!.*\\d{8}\\.html$) - if there is any 0+ chars other than line break chars, as many as possible, then 8 digits and .html at the end of string, fail the match
  • .*$ - any 0+ chars other than line break chars, as many as possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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