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

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

Expand Messages
  • Chuck Mason
    Am I the only one amused that /home n does nearly the same thing? I mean, I suppose you could be doing operations on your search but couldn t you equally
    Message 1 of 20 , Nov 30, 2006
    • 0 Attachment
      Am I the only one amused that /home<CR>n does nearly the same thing?
      I mean, I suppose you could be doing operations on your search but
      couldn't you equally just "ayw and do stuff there? Besides \{-}\zs
      takes too long to type :)

      -----Original Message-----
      From: Bill McCarthy [mailto:WJMc@...]
      Sent: Thursday, November 30, 2006 4:56 PM
      To: zzapper
      Cc: vim@...
      Subject: Re: Matching JUST the nth occurence of a text in a line

      On Thu 30-Nov-06 6:31pm -0600, zzapper wrote:

      > Hi given a sample text below
      >
      > home
      >
      > home gg home xxx
      >
      > home ggg ggg home home
      >
      > home home
      >
      > How do you match JUST the second "home" in any of the above lines (ie
      avoid
      > greediness)

      /home.\{-}\zshome

      --
      Best regards,
      Bill
    • A.J.Mechelynck
      ... He gave it. In the above case, you highlight not only the 2nd occurrence but also the 4th, 6th, etc. Best regards, Tony.
      Message 2 of 20 , Nov 30, 2006
      • 0 Attachment
        Bill McCarthy wrote:
        > On Thu 30-Nov-06 11:49pm -0600, Peter Hodge wrote:
        >> --- Bill McCarthy wrote:
        >>> On Thu 30-Nov-06 10:24pm -0600, Peter Hodge wrote:
        >>>> --- Bill McCarthy wrote:
        >>>>> On Thu 30-Nov-06 9:20pm -0600, 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
        >
        >>>>> One might think so, but also note:
        >>>>>
        >>>>> :help non-greedy
        >>>>>
        >>>>> In particular, ready the sentence starting with "BUT".
        >>>>> All that is needed is:
        >>>>>
        >>>>> /home.\{-}\zshome
        >>>>>
        >>>>> Earliest is preferred to shortest.
        >
        >>>> Yes, but that could also match a 3rd or 4th or 5th
        >>>> occurance of 'home' in one line, so it's slightly safer
        >>>> to anchor the pattern to the start of the line.
        >
        >>> Given the use of the "shortest match first algorithm" I
        >>> don't see how that's possible. Please give an example.
        >
        >> '/home.\{-}\zshome' will match every 2nd home in the
        >> following text:
        >>
        >> home home home home
        >> home home home home home home home home
        >> home home home home home
        >
        > Yes, I know it works. You stated that it was "safer to
        > anchor the pattern to the start of the line." I was asking
        > for a example of a failure of '/home.\{-}\zshome' that would
        > require such an anchor.
        >

        He gave it. In the above case, you highlight not only the 2nd occurrence but
        also the 4th, 6th, etc.


        Best regards,
        Tony.
      • A.J.Mechelynck
        ... To go to the 2nd occurrence from the cursor, you can use 2/home (or 2/ if you want to exclude homeless etc.) but all occurrences will be
        Message 3 of 20 , Dec 1, 2006
        • 0 Attachment
          Chuck Mason wrote:
          > Am I the only one amused that /home<CR>n does nearly the same thing?
          > I mean, I suppose you could be doing operations on your search but
          > couldn't you equally just "ayw and do stuff there? Besides \{-}\zs
          > takes too long to type :)

          To go to the 2nd occurrence from the cursor, you can use

          2/home

          (or 2/\<home\> if you want to exclude "homeless" etc.) but all occurrences
          will be highlighted.


          Best regards,
          Tony.
        • zzapper
          A.J.Mechelynck wrote in ... /^. {-}home. {-} zshome/e Was the solution I was looking for (BTW the /e indicates where the match
          Message 4 of 20 , Dec 1, 2006
          • 0 Attachment
            "A.J.Mechelynck" <antoine.mechelynck@...> wrote in
            news:456FE0F9.2080501@...:

            > Bill McCarthy wrote:
            >> On Thu 30-Nov-06 11:49pm -0600, Peter Hodge wrote:
            >>> --- Bill McCarthy wrote:
            >>>> On Thu 30-Nov-06 10:24pm -0600, Peter Hodge wrote:
            >>>>> --- Bill McCarthy wrote:
            >>>>>> On Thu 30-Nov-06 9:20pm -0600, Peter Hodge wrote:
            >>
            >>>>>>> Try:
            >>>>>>>
            >>>>>>> /^.\{-}home.\{-}\zshome
            >>>>>>>

            /^.\{-}home.\{-}\zshome/e

            Was the solution I was looking for (BTW the /e indicates where the match
            ends)

            Thanks
            --
            http://successtheory.com/tips/ Vim, Zsh, MySQL Tips
          • Charles E Campbell Jr
            ... 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,
            Message 5 of 20 , Dec 1, 2006
            • 0 Attachment
              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
            • 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 6 of 20 , Dec 1, 2006
              • 0 Attachment
                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 7 of 20 , Dec 1, 2006
                • 0 Attachment
                  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 8 of 20 , Dec 1, 2006
                  • 0 Attachment
                    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 9 of 20 , Dec 1, 2006
                    • 0 Attachment
                      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 10 of 20 , Dec 1, 2006
                      • 0 Attachment
                        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 11 of 20 , Dec 1, 2006
                        • 0 Attachment
                          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 12 of 20 , Dec 1, 2006
                          • 0 Attachment
                            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 13 of 20 , Dec 1, 2006
                            • 0 Attachment
                              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 14 of 20 , Dec 1, 2006
                              • 0 Attachment
                                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.