这个正则表达式匹配什么样的字符串?

DSh*_*per 2 regex

请给我一些与此正则表达式匹配的文本示例:

root/(.+)-go-to-products.php
Run Code Online (Sandbox Code Playgroud)

cod*_*ict 5

它匹配任何字符串,root/后面跟着除了换行符之后的一个或多个字符后跟-go-to-products随后的任何一个字符(除了换行符之外)php,这些字符串可以出现在字符串中的任何位置.

它会匹配:

root/f-go-to-products.php
root/foo-go-to-products.php
root/foo-go-to-products.php5     # Because you are not using anchor
http://stackoverflow.com/questions/3905066?root/f-go-to-products.php # no anchor
root/../foo-go-to-products.php   # . can match a literal . and /
Run Code Online (Sandbox Code Playgroud)

并且

root/foo-go-to-products-php      # because . is a meta char.
Run Code Online (Sandbox Code Playgroud)

但不是

root/-go-to-products.php         # because .+ expects at least 1 char.
Run Code Online (Sandbox Code Playgroud)

为了匹配.之前php字面意思逃脱它:root/(.+)-go-to-products\.php

此外,如果您正在使用正则表达式进行匹配,并且您不想提取匹配的内容.+,则可以删除括号并使用:

root/.+-go-to-products\.php

为了确保在找到模式作为子字符串时不会发生匹配,您应该锚定为: ^root/.+-go-to-products\.php$

由于.匹配文字.和a /,你的正则表达式可以匹配潜在的危险输入,如:root/../bar/foo-go-to-products.php.在这种输入PHP文件foo-go-to-products.php不存在于该root目录,但存在于root/../bar该目录是bar在相同的水平目录root.

  • 关于`products-php`的好看.我还要添加`root/foo/~bar /../../../ baz-go-to-products.php`,以表明各种其他有潜在危险的东西都被接受了. (2认同)