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

Explanation on \zs

Expand Messages
  • ACR
    Hi all, Can someone suggest a good method to learn the zs, ze, @
    Message 1 of 6 , Feb 28, 2008
    • 0 Attachment
      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
      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Tony Mechelynck
      ... 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
      Message 2 of 6 , Feb 28, 2008
      • 0 Attachment
        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


        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • ACR
        Thanks a lot Tony.. I am trying out this in the :substitute command. For eg. I can use ... To replace Hello, this string is foo. Please check to Hello,
        Message 3 of 6 , Feb 29, 2008
        • 0 Attachment
          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
          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • Benjamin Fritz
          ... I view zs, ze, @
          Message 4 of 6 , 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
            -~----------~----~----~----~------~----~------~--~---
          • Tony Mechelynck
            ACR wrote: [...] ... [...] I m not sure it can always be done, but there is an example under :help / @
            Message 5 of 6 , Feb 29, 2008
            • 0 Attachment
              ACR wrote:
              [...]
              > Also, would like to know who \zs can replace \@<= in this command and
              > the result be the same.
              >
              > Thanks,
              > Anith.
              [...]

              I'm not sure it can always be done, but there is an example under ":help
              /\@<=". I can paraphrase it here, saying that /\%(\<something \)\@<=else\>/ is
              equivalent to /\<something \zselse\>/ (match "else" when part of "something
              else" with word breaks around them).


              Best regards,
              Tony.
              --
              It was the next morning that the armies of Twodor marched east
              laden with long lances, sharp swords, and death-dealing hangovers. The
              thousands were led by Arrowroot, who sat limply in his sidesaddle,
              nursing a whopper. Goodgulf, Gimlet, and the rest rode by him, praying
              for their fate to be quick, painless, and if possible, someone else's.
              Many an hour the armies forged ahead, the war-merinos bleating
              under their heavy burdens and the soldiers bleating under their melting
              icepacks.
              -- The Harvard Lampoon, "Bored of the Rings"


              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • Yongwei Wu
              ... Please notice the Vim mailing list requires bottom-posting. My understanding of what happens in your matching: - . is matched first; - For each match of
              Message 6 of 6 , Mar 1 6:38 AM
              • 0 Attachment
                On 29/02/2008, 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?

                Please notice the Vim mailing list requires bottom-posting.

                My understanding of what happens in your matching:

                - "." is matched first;
                - For each match of "." (i.e., every character), check whether "foo.*"
                is before it;
                - If "foo.*" is before it, replace "." with "-".

                So it is a slow matching process, just as the help says.

                > Also, would like to know who \zs can replace \@<= in this command and
                > the result be the same.

                I think you just had an example where \zs cannot replace \@<=. The
                similar substitution "%s/\(foo.*\)\zs./-/g" work differently: it will
                match "foo.*.", and replace the last "." with "-".

                Best regards,

                Yongwei

                --
                Wu Yongwei
                URL: http://wyw.dcweb.cn/

                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              Your message has been successfully submitted and would be delivered to recipients shortly.