tags:

views:

53

answers:

2

Please give me examples of text which match this regex:

root/(.+)-go-to-products.php
+4  A: 

It will match anything that contains with "root/", followed by something, followed by "-go-to-products", followed by a character, followed by "php".

root/a-go-to-products.php //Okay
root/a-go-to-products.ph  //Not okay
a-go-to-products.phproot/ //Not okay
root/-go-to-products.php  //Not okay
root/a-go-to-products php //Okay

You might want to escape the last . for it to capture only the dot character.

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

Resources :

Colin Hebert
+5  A: 

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.

codaddict
Nice catch about `products-php`. I'd add `root/foo/~bar/../../../baz-go-to-products.php` as well, to show that all kinds of other potentially dangerous stuff gets accepted.
Thilo
You can make any url match that regex: `anypage.php?dummy=root/f-go-to-products.php`
Kobi
@Kobi even with anchors it can fail: `root/index.php?dummy=-go-to-products.php`
Ishtar
`http://stackoverflow.com/questions/3905066?root/f-go-to-products.php`
Thilo
@Thilo: Thanks for the inputs. I've updated the answer.
codaddict