Regular expression to match only root resources

I do not have enough knowledge about regular expressions and am trying to solve the following problem.

I want to write an expression to match only HTML files (* .html) in the root path of the web application. For instance.

The following should match:

/myfile.html

Below should not match (contains a subdirectory):

/home/myfile.html

My current expression is:

^ / (. *. HTML) $

is not satisfactory because it will correspond to files in subdirectories.

Can anyone solve this problem for me?

Thank,

Andrew

+3
source share
5 answers

Try the following:

^/([^/]+\.html)$

[^/]+ will match any other than /

html , ,
(. )

+4

- .*:

^/([^/]*\.html)$

, html, -, html, /testhtml.

+3

[^/] . , , . , , .

^/([^/]*\.html)$.

+2

You want to exclude slashes from the (. *) Part of the expression. In addition, you should avoid the dot that introduces the file extension. Otherwise, it will match any character.

^/([^/]*\.html)$
+2
source

Add ^ /, which means that the slash inside the string will not match the regular expression.

0
source

Source: https://habr.com/ru/post/1790000/


All Articles