RegEx Replace?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan M Dunsmuir

    #1

    RegEx Replace?

    I am confused by the operation of the RegEx Replace functions provided
    by PHP.

    I understand, of course, what is intended if the 'Replace' string is a
    simple character string, to be slotted into the target whenever a match
    is achieved.

    But what is intended when the 'Replace' string is itself a RegEx string?

    How does that work?
  • Franz Marksteiner

    #2
    Re: RegEx Replace?

    Alan M Dunsmuir wrote:
    I am confused by the operation of the RegEx Replace functions provided
    by PHP.
    >
    I understand, of course, what is intended if the 'Replace' string is a
    simple character string, to be slotted into the target whenever a
    match is achieved.
    >
    But what is intended when the 'Replace' string is itself a RegEx
    string?
    How does that work?
    That is a little tricky indeed:
    You can address matching subpatterns from your RegExp in the replacement
    string.
    Thatfore you have to embrace those subpatterns within "()", which you can
    now use with $1, $2, ... in the replacement.

    --
    Freundliche Grüße,
    Franz Marksteiner

    Comment

    • Rik Wasmus

      #3
      Re: RegEx Replace?

      On Mon, 04 Feb 2008 14:48:37 +0100, Alan M Dunsmuir
      <alan@moonrake. demon.co.ukwrot e:
      I am confused by the operation of the RegEx Replace functions provided
      by PHP.
      >
      I understand, of course, what is intended if the 'Replace' string is a
      simple character string, to be slotted into the target whenever a match
      is achieved.
      >
      But what is intended when the 'Replace' string is itself a RegEx string?
      What do you mean with 'the replace string is a regex string'? Could you
      give an example for this? If you're replacment string could be interpreted
      as a regex this won't do a thing, it will be 'just another string' as far
      as PHP is concerned.

      If you want to do further processing on a matched string, you could use
      the subpatterns with $1, $2 etc. (be sure to use _single_ quotes around
      the string). If the operation is more complex, you can use either the /e
      modifier or preg_replace_ca llback() to use a (custom) function to perform
      more operations on a string.
      --
      Rik Wasmus

      Comment

      • Alan M Dunsmuir

        #4
        Re: RegEx Replace?

        Franz Marksteiner wrote:
        >
        That is a little tricky indeed:
        You can address matching subpatterns from your RegExp in the replacement
        string.
        Thatfore you have to embrace those subpatterns within "()", which you
        can now use with $1, $2, ... in the replacement.
        >
        I clearly don't have the hang of it yet.

        I tried (using Forta's RegEx Tester) with the RegEx ([0-9]{3})[0-9]{2}
        and the replacement string ($1), offering it the text 1asc1234wer9876 5
        and expecting to see 987.

        In fact I got 1asc1234wer(987 ) and realised that non-matches must
        survive unchanged.

        But where did the ( and ) come from?



        Comment

        • Alan M Dunsmuir

          #5
          Re: RegEx Replace?

          Rik Wasmus wrote:
          I expect you did this:
          >
          preg_replace('/([0-9]{3})[0-9]{2}/','($1)','1asc1 234wer98765');
          (if not, don't make use guess, _give the code_)
          I didn't use code directly. I used the Forta RegEx Tester
          (http://www.forta.com/books/0672325667/), which is a downloadable applet
          with available flavours .NET, PHP, JavaScript, etc.

          For convenient I used the JavaScript version on Firefox.

          Comment

          • Franz Marksteiner

            #6
            Re: RegEx Replace?

            Alan M Dunsmuir wrote:
            Rik Wasmus wrote:
            >I expect you did this:
            >>
            >preg_replace ('/([0-9]{3})[0-9]{2}/','($1)','1asc1 234wer98765');
            >(if not, don't make use guess, _give the code_)
            >
            I didn't use code directly. I used the Forta RegEx Tester
            (http://www.forta.com/books/0672325667/), which is a downloadable
            applet with available flavours .NET, PHP, JavaScript, etc.
            >
            For convenient I used the JavaScript version on Firefox.
            Don`t worry, just leave out the "()" in the replacement string.

            --
            Freundliche Grüße,
            Franz Marksteiner

            Comment

            Working...