• question about

    From alex@21:1/5 to All on Wed Feb 23 15:54:21 2022
    $ 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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kristjan Robam@21:1/5 to All on Mon Apr 4 04:06:27 2022
    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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to J.O. Aho on Mon Apr 4 13:07:59 2022
    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"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From J.O. Aho@21:1/5 to alex on Mon Apr 4 14:26:01 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Lew Pitcher on Mon Apr 4 13:29:58 2022
    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"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)