Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of strings does this regex match?

Tags:

regex

Please give me examples of text which match this regex:

root/(.+)-go-to-products.php
like image 951
DSharper Avatar asked Dec 05 '25 20:12

DSharper


1 Answers

It matches any string that has root/ followed by one ore more characters other than newline followed by -go-to-products followed by any one char(other than newline) followed by php and these can occur anywhere in the string.

It'll match:

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 /

and also

root/foo-go-to-products-php      # because . is a meta char.

But not

root/-go-to-products.php         # because .+ expects at least 1 char.

To match the . before php literally escape it as: root/(.+)-go-to-products\.php

Also if you are using the regex just for matching and you don't want to extract what is matched by .+ you can drop the parenthesis and just use:

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

To ensure a match does not happen when the pattern is found as a substring you should anchors as: ^root/.+-go-to-products\.php$

Since a . matches a literal . and a /, your regex can match potentially dangerous inputs like: root/../bar/foo-go-to-products.php. In this input the php file foo-go-to-products.php is not present in the root directory but is present in the root/../bar directory which is bar directory at the same level as root.

like image 111
codaddict Avatar answered Dec 08 '25 12:12

codaddict



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!