Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #18906

Re: question about

From Lew Pitcher <lew.pitcher@digitalfreehold.ca>
Newsgroups comp.lang.php
Subject Re: question about
Date 2022-04-04 13:07 +0000
Organization A noiseless patient Spider
Message-ID <t2eqjf$946$1@dont-email.me> (permalink)
References <sv5hqt$h8m$1@gioia.aioe.org> <jb06eqFiv7oU1@mid.individual.net>

Show all headers | View raw


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"

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web