Loading ...
Sorry, an error occurred while loading the content.
 

Re: [Clip] RegEx Replace

Expand Messages
  • Don
    ... Happy New Year Sheri (and everybody else too)! So if I matching anything without the K is it always in $0? If I only have one pattern and don t use K
    Message 1 of 14 , Jan 25, 2011
      > When you have \K in a pattern, first an entire match of the pattern is
      > located in the subject. Then the starting position of the match is reset
      > to the part of the subject that matches after the \K. Putting
      > parentheses around all that follows \K is not necessary, because what
      > follows the \K (by definition) going to be the whole match. The whole
      > match is automatically captured and can be referenced as $0 in the
      > replacement text.
      >
      > Regards,
      > Sheri

      Happy New Year Sheri (and everybody else too)!

      So if I matching anything without the \K is it always in $0?
      If I only have one pattern and don't use \K then I don't need
      parenthesis at all in other words?

      Don
    • Lee Underwood
      Thanks for the explanation, Axel. I appreciate it! ... and all the others too. Lee
      Message 2 of 14 , Jan 26, 2011
        Thanks for the explanation, Axel. I appreciate it! ... and all the others too.

        Lee


        > Lee Underwood wrote:
        > > What exactly does the "$" do? Wouldn't
        > >
        > > ^!Replace "<p>" >> "<p> " WRASTI
        > >
        > > work just as good?
        >
        > Not in the case as specified (and in your example you'd have to leave
        > out the R, i.e. WASTI not WRASTI). Graham did not want to replace ALL
        > <p> but only those directly followed by a digit.
        > So I look for "any digit" i.e. \d. This now becomes part of the search
        > term an will get replaced, meaning it will be lost if my replace is only
        > "<p>  ". By placing it in (the first pair of) brackets, Regex will
        > remember it as term number one. So I need to add "term number one" to
        > the replace string, which is exactly what $1 stands for. Which leads us
        > straight back to my formulation
        > ^!Replace "<p>(\d)" >> "<p>  $1" WRASTI
        >
        > As always there are alternatives in Regex. You can say "look for
        > <something> when followed by <soemthing else> and do not make <something
        > else> part of the search term". This can be quite useful and diodem uses
        > it a lot, but I can't remember the details and syntax.
        >
        > A third alternative would be
        > ^!Replace "(<p>)(\d)" >> "<p>  " WRASTI1
        > The two pairs of brackets define term one and term two and the one in
        > WRASTI1 (I hope your font lets you see a capital i followed by a digit
        > 1) says "only replace term one and leave the rest alone".
        >
        > As usual the version I have used is the most primitive one.
        >
        > Axel
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.