0

I'm trying to make a regex that matches with a text like /es/whathever1/whathever2/whatever3 and not ends with html

I tried with :

\/es\/.*[aA-zZ\-\_]\/.*[aA-zZ\-\_]\/.*[aA-zZ\-\_].*[.]html$.*$

but only matches if ends with .html

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
aa_cr
  • 11
  • 3
  • This will match it `^\/es(?:\/[\w-]+)+$` https://regex101.com/r/blUygJ/1 Using `aA-zZ` in a character class in not the same as A-Za-z. And note that you actually are matching `.html` at the end. – The fourth bird Jun 15 '21 at 10:51
  • `^\/es(?:\/[\w-]+)+$` matches with `/whathever` and `whatever/whatever` it only has to match with thre sections – aa_cr Jun 15 '21 at 11:02
  • Then you can try `^\/es(?:\/[\w-]+){3}$` https://regex101.com/r/eGUrLN/1 – The fourth bird Jun 15 '21 at 11:03

1 Answers1

2

Using the character class [aA-zZ\-\_] matches a single character of one of the listed. It is not the same as [a-zA-Z] as A-z matches more characters.

You can repeat 3 times matching / and 1+ word characters using a quantifier and add anchors for the start ^ and end $ of the string to prevent a partial match

^\/es(?:\/[\w-]+){3}$

See a regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • I'm guessing, but I think they're trying to match path names of files. And they just want to not match .html files. So f.ex. .txt and .pdf should still be a match. – Scratte Jun 15 '21 at 13:32
  • @Scratte I would not know about that part. If I remembered correctly, the OP replied that it worked but Is don't see that comment anymore now. – The fourth bird Jun 15 '21 at 13:35
  • I think that is your regex: [Redirect adding .html on .htaccess](https://stackoverflow.com/questions/67986281/redirect-adding-html-on-htaccess) – Scratte Jun 15 '21 at 13:42
  • 3
    @Scratte Ah, it is :-) Let it then be 2 separate questions to not mix answers. – The fourth bird Jun 15 '21 at 13:45