I’m trying to use a regular expression to find the “system” folder within my application path, and replace it with “counters”.
but my regular expression patter is not happy with me.
My string that represents my app path is called $counterslocation and it looks like this:
“/var/www/myapp/system”
Ultimately, I would like to end up with this:
/var/www/myapp/counters
To start, I am using preg_match to make sure my pattern is correct. Then I will try preg_replace. I have the following code for the regex:
$pattern='/(\/[0-9a-z]\/)*(myapp\/)(system)/i';
if (preg_match($pattern,$counterslocation,$matches)) {
var_dump($matches);
}
The var_dump() call returns the following output:
array(4) { [0]=> string(10) “myapp/system” [1]=> string(0) “” [2]=>
string(4) “myapp/” [3]=> string(6) “system”
My question is, how do I get the regular expression to also return everything leading up to the “myapp” part of the string? I thought by putting it into parenthesis, it would capture it as the first group… but it looks like I’m doing something wrong.
Thanks.
You have not quantified the directory name and as a result it matched directory with single alpha-numeric character. You need: