Question: How can I check if specific path match to pattern.

Example:

I have a path with one or more unknown variable

```
$pathPattern = 'user/?/stats';

```

And let say I received this path

```
$receivedPath = 'user/12/stats'

```

So, how can I check if that received path match to my pattern?

I tried to do something like below but didn't work.

```
 $pathPattern = 'user/?/stats';
 $receivedPath = 'user/12/stats';

 $pathPatternReg = str_replace('?','.*',$pathPattern);
 echo preg_match('/$pathPatternReg/', $receivedPath);

```

Thank you.

Answer: 
Regex should be something like this for a unknown `user\/[0-9]+\/stats`

And Could be used as such;

`if(preg_match("user\/[0-9]+\/stats",$variable)) { .... }`
As stated by Tom, you possibly have to escape the '/' characters with a '\'.

You only want to match one specific part of your total query string, the number in the center. Most regex interpreters provide this functionality in form of round brackets, like this:

```
$pattern = "user\/([0-9]+)\/stats";

```

Notice the round brackets around the [0-9]+ : it tells preg\_match to store this part of the matched pattern in the $matches array.

So, your code could look like this:

```
$subject = "user/12/stats";
$pattern = "user\/([0-9]+)\/stats";
$matches = array();
if( preg_match($pattern, $subject, $matches) ){
    // there was a match
    // The $matches array now looks like this:
    // { "user/12/stats", "12" }
    // { <whole matched string>, <string in first parenthesis>, .... }
    $user_id = $matches[1]
    ...
}

```

(not tested)
See also here: <https://secure.php.net/manual/en/function.preg-match.php>