File:
C:\Some\Location\index
I just want to replace \index with \example
echo preg_replace('/[\\\|\/]{1}.*?$/', '\example', $file);
It just keeps being too greedy. I don't know of any modifiers that would help that problem.
Thanks!
Replace the . with [^/\], which matches anything but the slashes. I also changed the regex delimiter to @, since slashes aren't appropriate when handling paths.
$str='C:\\Some\\Location\\index';
echo preg_replace('@[/\\\\][^/\\\\]+$@', '\example', $str);
# echoes: C:\Some\Location\example
You don't need a regex for that if you can use forward slashes instead of those ugly backslashes (yes, windows does support forward slashes):
$str = 'C:/Some/Location/index';
echo dirname($str).'/example';