1

Let's say a buffer has a certain characters I know will be repeated seven (7) times. How can I search for any character repeated seven times?

I know I can search for .., but those to characters won't necessarily be the same. There's also a special character (not sure if it's &) that can be used in substitutions to paste the found string in the replacement field, but I'm also not sure if that's useable again here.

EDIT: I'm specifically searching for merge conflict markers from git (<<<<<<<, =======, or >>>>>>> - possibly |||||||), but I'd like to know how to perform this search for any character X, repeating N times, so that I may reapply the knowledge in the future.

Thanks to @DJMcMayhem for the suggestion of vimregex.com; it lead me to search for /\v<<<<<|=====|>>>>> which caused an error, as each of those are special characters. Escaping those, however (/\v\<\<\<\<\<|\=\=\=\=\=|\>\>\>\>\>) did succeed in finding what I was looking for, in the short term. I'm still looking for a way to search for character X, repeating N times.

6

There's two different regex features that would be helpful. First off, you can use capturing groups. If you put something in parentheses, you can refer to whatever matched inside. So

/\(.\)\1

Will match any character (\(.\)) followed by that same character (Group 1, \1). You can also refer to groups in the replacement part of a substitute command.

The other feature is quantifiers.

  • \{n,m} matches from n to m of the preceding characters...

  • \{n} matches exactly n times of the preceding characters...

  • \{,m} matches at most m (from 0 to m) of the preceding characters...

  • \{n,} matches at least n of of the preceding characters...

putting it all together, you want to use:

/\(.\)\1\{6}

Or as the way I'd prefer it,

/\v(.)\1{6}

I'd recommend reading through vimregex.com. It's a good tutorial on vim regex features.

  • This didn't seem to work (either with or without that final ` that seems to be optional). I am searching for git diff indicators, which are <<<<<<<, =======, or >>>>>>>. {I believe |||||||` is also used if it is a three-way merge, but don't know for sure.} Your suggestion of vimregex.com was valuable though; see my updated question. – user3.1415927 Mar 23 at 2:58
  • 1
    @user3.1415927 I'm not sure what to tell you. /\(.\)\1\{6} will definitely match any one character repeated 7 times, so there must be something else at play. And I did answer the question "How do I search for a character repeated N times": use \{n} Also your example regex doesn't seem right, each character is repeated only 5 times. – DJMcMayhem Mar 23 at 4:31
  • 1
    To add to that, your regex could be a lot cleaner if you used the quantifiers I mentioned. For example, /\v\<{7}|\={7}|\>{7} (or {5} if you want something equivalent to the one you posted) – DJMcMayhem Mar 23 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.