7

I want to find the words which contain the same string repeated twice. (e.g. wookokss(ok/ok), ccsssscc(ss/ss)). I think the expression is \(\w*\)\0.

Another try is to find the words which consist of the same string repeated twice. My answer is \<\(\w*\)\0\>. (word beginning + grouping(word) + group capture + word ending)

But they don't work. Could anybody help me?

1
  • 2
    I made 2 mistakes.One is using * because of empty string. Another is using \0 to represent the whole string.
    – Quexint
    Nov 14, 2015 at 10:07

3 Answers 3

5

To find a string repeated twice in a word, which is longer than two characters, you can use

/\(\w\{2,}\)\1

To match a whole word which contains beforementioned string, you can use

/\<\w\{-}\(\w\{2,}\)\1\w\{-}\>

Little bit of explanation

  • \1 - matches the same string that was matched by the first sub-expression in \( and \) (\0 matches the whole matched pattern)
  • \{n,} - matches at least n of the preceding atom, as many as possible
  • \{-} - matches 0 or more of the preceding atom, as few as possible
  • \w - the word character ([0-9A-Za-z_])
  • \< - the beginning of a word
  • \> - the end of a word

More in :help pattern

3

1.) words which contain the same string repeated twice. (e.g. wookokss(ok/ok),

To find words containing two or more repeated word characters try

\(\w\{2,}\)\1

\1 matches what's captured in first group.

2.) find the words which consist of the same string repeated twice...

To capture \w\+ one or more word characters followed by \1 what's captured in first group

\<\(\w\+\)\1\>

should be about it. Have a look at this tutorial.

1

For the first one use (.{2,})\1 example here: https://regex101.com/r/gK0mM2/2 That is assuming that you only look for duplicate strings that have more than 1 character.

and for the second one ^(.{2,})\1$ example here: https://regex101.com/r/lC2yT7/2

Edit: changed the second expression, it now also looks for strings with at least 2 characters

4
  • 4
    It's just too bad that the syntax used by the regex101 site has nothing to do with the syntax used by Vim regexes. Nov 14, 2015 at 9:15
  • @SatoKatsura, you might like to check out Vim Regular Expressions 101 then. vimregex.com Nov 16, 2015 at 19:24
  • @BrianTiffin (1) It doesn't make the answer correct, and (2) the vimregex.com site looks like a badly outdated introduction to Vim regexes (f.i. there's no mention of look-around assertions, or of the tremendously useful \zs and \ze). Nov 17, 2015 at 7:14
  • Points taken @SatoKatsura. Thanks for the warnings, I usually use that page for quick reminders, having very low levels of regex fu Nov 17, 2015 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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