简体   繁体   中英

Regex - All PHP files without the .html extension

I need a regex to get all files with the .php extension, but that don't contain .html .

For example:

foo.php (this file is ok)
foo.html.php (this file is not ok)
foo.bar (this file is not ok)
foo.bar.php (this file is ok)

I created this regex:

^(?!.*(.html))

But in this way I also get the files that end with .bar (and not only with .php ).

In your own regex you didn't explicitly specify the .php file extension, so the engine doesn't have any idea which extensions should match and which shouldn't. You are near to make it, and just a bit if modifications are needed:

^(?:.(?!\.html))*\.php$

Live demo

This will match the .php which is not preceded by html. I've used negative look behind to get it

.*(?<!\.html)\.php$

Explanation from regex101

(?<!html) Negative Lookbehind. Assert that the Regex below does not match html matches the characters html literally (case sensitive)

\\. matches the character . literally (case sensitive) php matches the characters php literally (case sensitive)

$ asserts position at the end of a line

https://regex101.com/r/TOiJQP/5

You can make sure there is no .html anywhere and that it finishes with .php in the string you're trying to match with:

^(?!.*\.html).*\.php$

https://regex101.com/r/TOiJQP/4

  • ^(?!.*\\.html) Negative lookahead. From the begining of the string (hence the ^ ), it makes sure there is no .html anywhere in the filename
  • .*\\.php$ matches a filename ending with .php

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