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

Re: Matching JUST the nth occurence of a text in a line

Expand Messages
  • mzyzik@gmail.com
    All, I am puzzled by a slightly more complicated version: how to match a % character following the 2nd occurrence of home ? --Matt
    Message 1 of 20 , Dec 1, 2006
      All,

      I am puzzled by a slightly more complicated version:
      how to match a '%' character following the 2nd occurrence of "home"?

      --Matt

      On Fri, Dec 01, 2006 at 09:09:17AM -0500, Charles E Campbell Jr wrote:
      > Peter Hodge wrote:
      >
      > >Try:
      > >
      > > /^.\{-}home.\{-}\zshome
      > >
      > >
      > >for your reference:
      > >
      > > \{-} makes the '.' match as little as possible
      > > \zs makes the search match begin at this point in the pattern
      > >
      > >
      > >
      > To generalize to the n-th occurrence: (put the qty of skipped matches in N)
      >
      > /^.\{-}\%(home.\{-1,}\)\{N}\zshome
      >
      > So, for the 2nd home: let N=1.
      >
      > Regards,
      > Chip Campbell
      >
    • Charles E Campbell Jr
      ... /^. {-} %(home. {-1,} ) {N}home. {-} zs% where N is 1 for the 2nd occurrence (N is 2 for the third occurrence, etc). This pattern matches up to and
      Message 2 of 20 , Dec 1, 2006
        mzyzik@... wrote:

        >I am puzzled by a slightly more complicated version:
        > how to match a '%' character following the 2nd occurrence of "home"?
        >
        >
        /^.\{-}\%(home.\{-1,}\)\{N}home.\{-}\zs%

        where N is 1 for the "2nd occurrence" (N is 2 for the third occurrence,
        etc).

        This pattern matches up to and including the 2nd home, extends past a
        minimal amount of anything up to but not including a %, whereupon the
        pattern matching starts.

        Regards,
        Chip Campbell
      • mzyzik@gmail.com
        ... This pattern doesn t work. Try it on this line: home home home home home home% home It should match nothing on that line, because the % doesn t follow
        Message 3 of 20 , Dec 1, 2006
          On Fri, Dec 01, 2006 at 10:34:14AM -0500, Charles E Campbell Jr wrote:
          > mzyzik@... wrote:
          >
          > >I am puzzled by a slightly more complicated version:
          > > how to match a '%' character following the 2nd occurrence of "home"?
          > >
          > >
          > /^.\{-}\%(home.\{-1,}\)\{N}home.\{-}\zs%

          This pattern doesn't work. Try it on this line:
          "home home home home home home% home"

          It should match nothing on that line, because the '%' doesn't follow the
          2nd occurrence as desired.

          --Matt

          >
          > where N is 1 for the "2nd occurrence" (N is 2 for the third occurrence,
          > etc).
          >
          > This pattern matches up to and including the 2nd home, extends past a
          > minimal amount of anything up to but not including a %, whereupon the
          > pattern matching starts.
          >
          > Regards,
          > Chip Campbell
          >
        • Charles E Campbell Jr
          ... But it *does* work! You didn t specify avoiding the % if matches to the home pattern intervened. Try ... and the following two lines: home % home home
          Message 4 of 20 , Dec 1, 2006
            mzyzik@... wrote:

            >On Fri, Dec 01, 2006 at 10:34:14AM -0500, Charles E Campbell Jr wrote:
            >
            >
            >>mzyzik@... wrote:
            >>
            >>
            >>
            >>>I am puzzled by a slightly more complicated version:
            >>> how to match a '%' character following the 2nd occurrence of "home"?
            >>>
            >>>
            >>>
            >>>
            >>/^.\{-}\%(home.\{-1,}\)\{N}home.\{-}\zs%
            >>
            >>
            >
            >This pattern doesn't work. Try it on this line:
            >"home home home home home home% home"
            >
            >It should match nothing on that line, because the '%' doesn't follow the
            >2nd occurrence as desired.
            >
            >
            But it *does* work! You didn't specify avoiding the % if matches to the
            home pattern intervened.
            Try
            :set hls
            and the following two lines:

            home % home home home home home% home
            home home %home home home% home

            and you'll only see %s match after the 2nd home, exactly as specified.
            Now, if you change
            the requirement, a different regex is needed.

            Regards,
            Chip Campbell
          • mzyzik@gmail.com
            ... I can rephrase: how to match a % character following the 2nd occurrence of home ? to how to match a % character directly following the 2nd
            Message 5 of 20 , Dec 1, 2006
              On Fri, Dec 01, 2006 at 04:41:37PM -0500, Charles E Campbell Jr wrote:
              > >>>I am puzzled by a slightly more complicated version:
              > >>> how to match a '%' character following the 2nd occurrence of "home"?
              > >>/^.\{-}\%(home.\{-1,}\)\{N}home.\{-}\zs%
              > >This pattern doesn't work. Try it on this line:
              > >"home home home home home home% home"
              > But it *does* work! You didn't specify avoiding the % if matches to the
              > home pattern intervened.

              I can rephrase:
              "how to match a '%' character following the 2nd occurrence of "home"?"
              to
              "how to match a '%' character directly following the 2nd occurrence of "home"?"

              I am pretty sure everyone else knew what I meant.

              --Matt

              P.S. Does adding "directly" change the meaning for you? And if so, do
              you know the regular expression I want?

              > Try
              > :set hls
              > and the following two lines:
              >
              > home % home home home home home% home
              > home home %home home home% home
              >
              > and you'll only see %s match after the 2nd home, exactly as specified.
              > Now, if you change
              > the requirement, a different regex is needed.
              >
              > Regards,
              > Chip Campbell
              >
            • Yakov Lerner
              ... I don t have tag non-greedy in the help. I have vim 7.9.164 from svn. Where do I get more updated helpfiles ? Yakov
              Message 6 of 20 , Dec 1, 2006
                On 12/1/06, Bill McCarthy <WJMc@...> wrote:
                > One might think so, but also note:
                >
                > :help non-greedy

                I don't have tag 'non-greedy' in the help. I have vim 7.9.164
                from svn. Where do I get more updated helpfiles ?

                Yakov
              • Yakov Lerner
                ... 7.0.164, sorry, not 7.9.164. Still, where do I get more updated helpfiles like yours ? Yakov
                Message 7 of 20 , Dec 1, 2006
                  On 12/2/06, Yakov Lerner <iler.ml@...> wrote:
                  > On 12/1/06, Bill McCarthy <WJMc@...> wrote:
                  > > One might think so, but also note:
                  > >
                  > > :help non-greedy
                  >
                  > I don't have tag 'non-greedy' in the help. I have vim 7.9.164
                  7.0.164, sorry, not 7.9.164. Still, where do I get more updated
                  helpfiles like yours ?
                  Yakov
                • A.J.Mechelynck
                  ... By ftp: ftp://ftp.vim.org/pub/vim/runtime/ and all its contents except its dos/ subdirectory. By rsync: the following assumes that you compile vim
                  Message 8 of 20 , Dec 1, 2006
                    Yakov Lerner wrote:
                    > On 12/1/06, Bill McCarthy <WJMc@...> wrote:
                    >> One might think so, but also note:
                    >>
                    >> :help non-greedy
                    >
                    > I don't have tag 'non-greedy' in the help. I have vim 7.9.164
                    > from svn. Where do I get more updated helpfiles ?
                    >
                    > Yakov
                    >

                    By ftp:

                    ftp://ftp.vim.org/pub/vim/runtime/

                    and all its contents except its dos/ subdirectory.

                    By rsync: the following assumes that you compile vim yourself, and that the
                    current directory is the top of your building tree: src/.. if you will. The
                    two additional lines copy (on Unix) the updated files to where you'll be using
                    them (such as /usr/local/share/vim/vim70/).

                    rsync -avzcP --delete --exclude="/dos/" ftp.nluug.nl::Vim/runtime/ ./runtime/
                    cd src
                    make installruntime

                    Either method updates all your runtime files. In addition, rsync removes
                    outdated files which don't exist anymore in the latest version of the files,
                    and it doesn't touch the files which are already up-to-date.


                    Best regards,
                    Tony.
                  • Bill McCarthy
                    ... I see Tony already provided the an approach for unix. To update your directories on Windows (using 4nt): copy /[!*~]/u/s
                    Message 9 of 20 , Dec 1, 2006
                      On Fri 1-Dec-06 7:19pm -0600, Yakov Lerner wrote:
                      > On 12/1/06, Bill McCarthy <WJMc@...> wrote:

                      >> One might think so, but also note:
                      >>
                      >> :help non-greedy

                      > I don't have tag 'non-greedy' in the help. I have vim 7.9.164
                      > from svn. Where do I get more updated helpfiles ?

                      I see Tony already provided the an approach for unix. To
                      update your directories on Windows (using 4nt):

                      copy /[!*~]/u/s "ftp://ftp.vim.org/pub/vim/runtime/dos" c:\vim\vim70

                      adjusting the target as appropriate. This copies only files
                      that are newer or didn't exist on the target. It skips
                      files ending with a tilde.

                      And if any help files were updated:

                      vim -nes "+helpt c:\vim\vim70\doc|q"

                      --
                      Best regards,
                      Bill
                    Your message has been successfully submitted and would be delivered to recipients shortly.