Mark
Mark

Reputation: 43

PHP, Incosistent results with preg_replace (RegEx)

I'm looking for a nudge in the right direction because the results I get from preg_replace don't make sense to me.

I've the following RegEx:

/([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,})/([a-zA-Z0-9_]{1,})/([a-zA-Z0-9_]{1,})/([a-zA-Z0-9_]{1,})\b

I've a file which consists of lines like this:

1: 

*/tdn/quota/plot_3/boot_tdd_8/Homes_Homes1/boot/bplsed/ruc001/No Files/pl1/Cookies/MMTException/container.rig,11/12/2017,29/11/2017,29/11/2017*

2:

*/vdm/quota/plot_1/boot_tdd_1/Homes_Homes2/.etc/nonrig_tile_edit.vids,07/08/2014,07/08/2014,07/08/2014*

3:

*/vdm/quota/plot_5/boot_tdd_3/Homes_Homes1/boot/int/rlt111/pl1/Cookies/container.rig,19/11/2019,13/11/2017,13/11/2017*

My goal is to only keep everything after the /Homes_Homes/ part.

I get the correct replacement for the first file path with my Regex:

*/boot/bplsed/ruc001/No Files/pl1/Cookies/MMTException/container.rig,11/12/2017,29/11/2017,29/11/2017*

The second file path is also correct:

*/.etc/nonrig_tile_edit.vids,07/08/2014,07/08/2014,07/08/2014*

However, for the last file path I get:

*/container.rig* 

instead of

*/boot/int/rlt111/pl1/Cookies/container.rig,19/11/2019,13/11/2017,13/11/2017*

Why does preg_replace fail with the third file path?

Upvotes: 0

Views: 27

Answers (1)

The fourth bird
The fourth bird

Reputation: 163362

The reason you get that last result is because preg_replace will replace all the matches and in the last example string the pattern matches twice.

What you might do is set the 4th parameter $limit to 1 to do a single replacement.

Not all the character classes in your pattern match an underscore, but if it would be ok to do so, you might shorten the pattern using a quantifier {5}, and anchor ^ to assert the start of the srting and make use of \K to match and then forget the * at the start of the string.

^\*\K(?:/\w+){5}

Regex demo | Php demo

For example

$re = '~^\*\K(?:/\w+){5}~';
$strings = [
    "*/tdn/quota/plot_3/boot_tdd_8/Homes_Homes1/boot/bplsed/ruc001/No Files/pl1/Cookies/MMTException/container.rig,11/12/2017,29/11/2017,29/11/2017*",
    "*/vdm/quota/plot_1/boot_tdd_1/Homes_Homes2/.etc/nonrig_tile_edit.vids,07/08/2014,07/08/2014,07/08/2014*",
    "*/vdm/quota/plot_5/boot_tdd_3/Homes_Homes1/boot/int/rlt111/pl1/Cookies/container.rig,19/11/2019,13/11/2017,13/11/2017*"
];

foreach ($strings as $s) {
    echo preg_replace($re, '', $s) . PHP_EOL;
}

Output

*/boot/bplsed/ruc001/No Files/pl1/Cookies/MMTException/container.rig,11/12/2017,29/11/2017,29/11/2017*
*/.etc/nonrig_tile_edit.vids,07/08/2014,07/08/2014,07/08/2014*
*/boot/int/rlt111/pl1/Cookies/container.rig,19/11/2019,13/11/2017,13/11/2017*

Upvotes: 1

Related Questions