$ echo center | sed -E 's#(.*)#start-\1-end#'
start-center-end
I tried to do the same thing with php, but the result is different
$ php -r "echo preg_replace('#(.*)#', 'start-\0-end', 'center') . PHP_EOL;" start-center-endstart--end
^^^^^^^^^^
Why?
On 23/02/2022 15.54, alex wrote:
$ echo center | sed -E 's#(.*)#start-\1-end#'
start-center-end
I tried to do the same thing with php, but the result is different
$ php -r "echo preg_replace('#(.*)#', 'start-\0-end', 'center') . PHP_EOL;" >> start-center-endstart--end
^^^^^^^^^^
Why?
Why I'm not sure,
but figured out two solutions:
You set a number a limit to the replacement
$ php -r "echo preg_replace('#(.*)#', 'start-\1-end', 'center', 1) . PHP_EOL;"
or you can tell that it's from the beginning of the row
$ php -r "echo preg_replace('#^(.*)#', 'start-\1-end', 'center') . PHP_EOL;
I would guess this has to do with the end of char array \0 (internally)
and it's counted as a white space, so it's not included in the first
match, but then as the second match, but that is just a wild guess.
$ echo center | sed -E 's#(.*)#start-\1-end#'
start-center-end
I tried to do the same thing with php, but the result is different
$ php -r "echo preg_replace('#(.*)#', 'start-\0-end', 'center') . PHP_EOL;" start-center-endstart--end
^^^^^^^^^^
Why?
On Mon, 04 Apr 2022 14:26:01 +0200, J.O. Aho wrote:
On 23/02/2022 15.54, alex wrote:
$ echo center | sed -E 's#(.*)#start-\1-end#'
start-center-end
I tried to do the same thing with php, but the result is different
$ php -r "echo preg_replace('#(.*)#', 'start-\0-end', 'center') . PHP_EOL;" >>> start-center-endstart--end
^^^^^^^^^^
Why?
Why I'm not sure,
So, let's look at it and figure the "why" out, shall we?
The first three arguments to preg_replace() are
the regex pattern to test with
the substitution pattern to replace regex matches with, and
the string to be tested with the regex
In the OP's case, the regex pattern is
#(.*)#
where the # characters simply delimit the edges of the regex.
(Note that the convention is to use forward slashes, but PHP
preg_replace() accepts other characters as well. So, you might
normally see this pattern as /(.*)/ in other regex parsers.)
As the octothorpe signs are string delimiters rather than part
of the regex match string, the OP's regex devolves to
(.*)
which will match to ZERO OR MORE occurrences of ANY character.
The OP's substitution pattern is
start-\0-end
which instructs preg_replace() to replace the matched input
with ITSELF (\0), immediately preceded by "start-", and
immediately followed by "-end".
Finally, we get the OP's input string
center
Combine these two, and we get
- the regex #(.*)# matches the multi-character input string
'center'
- preg_replace() replaces 'center' with the interpreted
replacement string, resulting in an output of
begin-center-end
- the regex #(.*) matches EXACTLY ZERO characters in the
remaining input string
- preg_replace() replaces EXACTLY ZERO characters with
the interpreted replacement string, resulting in an
additional output of
begin--end
(note the EXACTLY ZERO characters copied from the input
to the gap between the 'begin-' portion and the '-end'
portion)
- the regex has run out of string, and preg_replace() stops.
but figured out two solutions:
You set a number a limit to the replacement
Thus telling preg_replace() to stop before it matches the
remaining nothing to (.*)
$ php -r "echo preg_replace('#(.*)#', 'start-\1-end', 'center', 1) .
PHP_EOL;"
or you can tell that it's from the beginning of the row
Thus telling preg_replace() to /only/ match everything up to and
including the remaining nothing at the end of the input.
$ php -r "echo preg_replace('#^(.*)#', 'start-\1-end', 'center') . PHP_EOL;
I would guess this has to do with the end of char array \0 (internally)
and it's counted as a white space, so it's not included in the first
match, but then as the second match, but that is just a wild guess.
HTH
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 349 |
Nodes: | 16 (2 / 14) |
Uptime: | 119:30:05 |
Calls: | 7,612 |
Files: | 12,787 |
Messages: | 5,684,032 |