How to search for multiple overlapping occurrences of the same word on the same line?

I want to search for all occurrences of a word in one line, as well as several files in a given file. For example:

ABCCG*CAT*AD*CAT*TT
DFGBBB*CAT*YYUAB

In a manual search for a word, 'CAT'I found two when used /CAT, when in fact there are three occurrences of this word in the file.

What is the command to search for all occurrences of a given word in a file, regardless of the fact that it can occur several times inside a line?

Note. There is no file *. I used it in the above example to indicate line positions CAT.

What if multiple occurrences should overlap on the same line? For example:

ABCCG*TNTNT*ADCATDD
DFGBBB*TNT*YYUAB

TNT :%s/TNT//gn 2, .

Vim?

+4
1

, "" , %s ( : %substitute) :

  • (n flag, "noop", )
  • (g "global" )
  • " " (\{-}; , , . ).

, :

:%s/[T]\{-}NT//gn

, :

ABCCG*TNTNT*ADCATDD
DFGBBB*TNT*YYUAB

... vim :

3 matches on 2 lines

/ , g, vim , . "" , \{-}.


vim . vim . :help count-items:

Counting words, lines, etc.                             count-items

To count how often any pattern occurs in the current buffer use the substitute
command and add the 'n' flag to avoid the substitution.  The reported number
of substitutions is the number of items.  Examples:

        :%s/./&/gn              characters
        :%s/\i\+/&/gn           words
        :%s/^//n                lines
        :%s/the/&/gn            "the" anywhere
        :%s/\<the\>/&/gn        "the" as a word

You might want to reset 'hlsearch' or do ":nohlsearch".
Add the 'e' flag if you don't want an error when there are no matches.

"" . :help non-greedy:

                                                        non-greedy
If a "-" appears immediately after the "{", then a shortest match
first algorithm is used (see example below).  In particular, "\{-}" is
the same as "*" but uses the shortest match first algorithm.  BUT: A
match that starts earlier is preferred over a shorter match: "a\{-}b"
matches "aaab" in "xaaab".

Example                 matches
ab\{2,3}c               "abbc" or "abbbc"
a\{5}                   "aaaaa"
ab\{2,}c                "abbc", "abbbc", "abbbbc", etc.
ab\{,3}c                "ac", "abc", "abbc" or "abbbc"
a[bc]\{3}d              "abbbd", "abbcd", "acbcd", "acccd", etc.
a\(bc\)\{1,2}d          "abcd" or "abcbcd"
a[bc]\{-}[cd]           "abc" in "abcd"
a[bc]*[cd]              "abcd" in "abcd"

The } may optionally be preceded with a backslash: \{n,m\}.
+4
source

Source: https://habr.com/ru/post/1606886/


All Articles