Regular Expressions: Replacing Regular Expression Matching Parts

Using Vim, Notepad ++ or Sublime, I would like to be able to search and replace parts of a match. I think this can be done using a negative outlook, but I would like to get opinions from the community.

Let's say I want to replace the “fallout” cases with “fallout” in the following examples:

  • They are concerned about nuclear attack and radiation.
  • There fall out, and then radiation.
  • Nagasaki has one of the biggest problems.
  • One of the best games of the 2000s "fell out."

Counter example:

  • This will go beyond your competence.

I believe that one obvious pattern matches:

fall out[^a-z]

"fallout" , .. , , - .

, ?

+4
1

, , , , , , , \> (. :h /\>), .

\> , ( buffer 'iskeyword').

, (. :h /\() fall out, .

:

:%s/\v\c<(fall)\s+(out)>/\1\2/g

:

             ┌──────── capture `fall`
             │       ┌ capture `out`
        ┌────┤   ┌───┤
%s/\v\c<(fall)\s+(out)>/\1\2/g
                         │ │
                         │ └─ use the text from the 2nd capturing group (will preserve the case)
                         └─ use the text from the 1st capturing group (will preserve the case)

\s+ ( ). \c , \v - . / .

Edit:

, , :

:%s/\v\c<fall\zs\s+\zeout>//g

:

%s/\v\c<fall\zs\s+\zeout>//g
            │     │
            │     └─ sets the end of the match
            └─ sets the start of the match

\zs \ze, . . :h /\zs :h /\ze.

+5

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


All Articles