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

87896Re: Explanation on \zs

Expand Messages
  • Benjamin Fritz
    Feb 29, 2008
    • 0 Attachment
      On 2/29/08, ACR <anith.ravi@...> wrote:
      >
      > Thanks a lot Tony..
      > I am trying out this in the :substitute command. For eg.
      > I can use
      > :%s/\(foo.*\)\@<=./-/g
      > To replace
      > "Hello, this string is foo. Please check" to "Hello, this string is
      > ---------------------------------"
      > But, am not able to explain the behaviour. How does this command
      > work?
      >
      > Also, would like to know who \zs can replace \@<= in this command and
      > the result be the same.
      >
      > Thanks,
      > Anith.
      >
      > On Feb 29, 11:26 am, Tony Mechelynck <antoine.mechely...@...>
      > wrote:
      >
      > > ACR wrote:
      > > > Hi all,
      > >
      > > > Can someone suggest a good method to learn the \zs, \ze, \@<=
      > > > options? I am trying to make sub-line modifications in my text
      > > > files. I am not quite following the :help \zs explanations. A more
      > > > detailed explanation or URL is welcome!
      > >
      > > > Thanks
      > > > ACR
      > >
      > > Here is how I understand the \zs and \ze atoms: First, match as if the pattern
      > > didn't contain them. Then, if found, move the start and end points of the
      > > "found text" (which will be highlighted with Search highlight, used as the
      > > value of \0 if the latter is found in the second term of a ":substitute",
      > > etc.) to \zs (if present) and \ze (if present), respectively.
      > >
      > > -- Well, sorry for the parentheses: maybe you'll have to omit them at first
      > > reading, then add them only if necessary for full details.
      > >
      > > \%(atom\)\@<= matches with zero length if \%(atom\) matches immediately
      > > before. But it is usually slow to match, even if backtracking will only go to
      > > the second nearest linebreak: use \zs instead, if possible.
      > >
      > > Best regards,
      > > Tony.
      > > --
      > > "If a camel flies, no one laughs if it doesn't get very far."
      > > -- Paul White

      I view \zs, \ze, \@<=, and the rest of the "zero-width" matches (see
      :help /zero-width) as "conditional matching". That is, match pattern
      b, but ONLY IF pattern a matches right before it (or right after, or
      doesn't match, etc.)

      Thinking of it in this fashion, the zero-width matches aren't actually
      part of the pattern (after all, they take no part in the replacement,
      highlight, etc) but instead impose limitations on exactly when the
      pattern can match.

      Another way of looking at it would be to go off of the term for them:
      zero width. So, you're going along, matching your regex, when you
      encounter one of these atoms. So, "rewind to the start" of the
      zero-width match and start over in your matching (though, note this
      can happen in the middle of a pattern for some of these, like \@!).

      It can be a little hard to wrap your mind around it, but once you do,
      zero-width matches are a very powerful tool.

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Show all 6 messages in this topic