Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression in Vim to match group capture

Tags:

regex

vim

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?

like image 710
Quexint Avatar asked Nov 14 '15 07:11

Quexint


People also ask

What is matching group in regex?

Regular expressions allow us to not just match text but also to extract information for further processing. This is done by defining groups of characters and capturing them using the special parentheses ( and ) metacharacters. Any subpattern inside a pair of parentheses will be captured as a group.

How do I match a pattern in Vim?

In normal mode, press / to start a search, then type the pattern ( \<i\> ), then press Enter. If you have an example of the word you want to find on screen, you do not need to enter a search pattern. Simply move the cursor anywhere within the word, then press * to search for the next occurrence of that whole word.

Does vim support regex?

VIM's syntax differs slightly from Perl's, but it's close enough. As a result, VIM users can benefit from Perl regular expression examples. In this article, we will discuss the most commonly used regular expressions in vim with examples. Let's get started.


2 Answers

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

like image 164
ryuichiro Avatar answered Nov 08 '22 16:11

ryuichiro


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.

like image 20
bobble bubble Avatar answered Nov 08 '22 16:11

bobble bubble