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

Re: [vimdev] Finding the nth occurrence of a string/char in a string

Expand Messages
  • Charles E. Campbell
    ... [^c] {-}c * this pattern matches a minimal amount of not-c up to the letter c {1} * will match up to one of the preceding regexp sub-expression
    Message 1 of 1 , Jan 3, 2002
    • 0 Attachment
      Thus saith Jeff Lanzarotta:
      > How would you find the nth occurrence of a string/char in a string?
      >
      > For example: If I have the string 'abcdabcdabcd' and I want to get the
      > offset to there the 2nd 'c' is, how would one do that?
      ---------------------------------------------------------------------

      [^c]\{-}c * this pattern matches a minimal amount of not-c up to
      the letter c
      \{1} * will match up to one of the preceding regexp
      sub-expression (ie. if you wanted to find the n'th,
      use \{n-1} here)
      \zs * anything before this is leading-context; sets start
      of match
      \(...\) * groups innards into a regexp sub-expression

      OK, with these regexp tools, let's put something together:

      /\([^c]\{-}c\)\{1}[^c]\{-}\zsc/

      Seems to work!
      Charles Campbell

      --
      Charles E Campbell, Jr, PhD _ __ __
      Goddard Space Flight Center / /_/\_\_/ /
      cec@... /_/ \/_//_/
      PGP public key: http://www.erols.com/astronaut/pgp.html
    Your message has been successfully submitted and would be delivered to recipients shortly.