Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #18864 > unrolled thread
| Started by | alex <1j9448a02@lnx159sneakemail.com.invalid> |
|---|---|
| First post | 2022-02-23 15:54 +0100 |
| Last post | 2022-04-04 13:29 +0000 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.php
question about alex <1j9448a02@lnx159sneakemail.com.invalid> - 2022-02-23 15:54 +0100
Re: question about Kristjan Robam <he6655442211@hotmail.com> - 2022-04-04 04:06 -0700
Re: question about "J.O. Aho" <user@example.net> - 2022-04-04 14:26 +0200
Re: question about Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-04-04 13:07 +0000
Re: question about Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-04-04 13:29 +0000
| From | alex <1j9448a02@lnx159sneakemail.com.invalid> |
|---|---|
| Date | 2022-02-23 15:54 +0100 |
| Subject | question about |
| Message-ID | <sv5hqt$h8m$1@gioia.aioe.org> |
$ 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?
[toc] | [next] | [standalone]
| From | Kristjan Robam <he6655442211@hotmail.com> |
|---|---|
| Date | 2022-04-04 04:06 -0700 |
| Message-ID | <eff37a28-9daa-4a5a-a5a7-fa1fcbb437abn@googlegroups.com> |
| In reply to | #18864 |
Something for You.
Can You help me out with 2100 euros ?
I want to buy a new Iphone.
Kristjan Robam
alex kirjutas Kolmapäev, 23. veebruar 2022 kl 16:54:33 UTC+2:
> $ 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?
[toc] | [prev] | [next] | [standalone]
| From | "J.O. Aho" <user@example.net> |
|---|---|
| Date | 2022-04-04 14:26 +0200 |
| Message-ID | <jb06eqFiv7oU1@mid.individual.net> |
| In reply to | #18864 |
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.
--
//Aho
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2022-04-04 13:07 +0000 |
| Message-ID | <t2eqjf$946$1@dont-email.me> |
| In reply to | #18904 |
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
--
Lew Pitcher
"In Skills, We Trust"
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2022-04-04 13:29 +0000 |
| Message-ID | <t2ersm$946$3@dont-email.me> |
| In reply to | #18906 |
On Mon, 04 Apr 2022 13:07:59 +0000, Lew Pitcher wrote:
> 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.
Oops... strike that. My response was for a different (and bad) solution
>> $ php -r "echo preg_replace('#^(.*)#', 'start-\1-end', 'center') . PHP_EOL;
Thus telling preg_replace() to /only/ match everything from the BEGINNING
of the input (the remaining nothing isn't at the beginning, so it doesn't
match).
>> 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
--
Lew Pitcher
"In Skills, We Trust"
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web