Consider the following example:

case Foo:
    ...
    break;
case Bar:
    ...
    break;
case More: case Complex:
    ...
    break:
...

Say, we would like to retrieve all matches of the regex case \([^:]*\): (the whole matching text or, even better, the part between \( and \)), which should give us (preferably in a new buffer) something like this:

Foo
Bar
More
Complex
...

Another example of a use case would be extraction of some fragments of an HTML file, for instance, image URLs.

Is there a simple way to collect all regex matches and take them out to a separate buffer in Vim?

Note: It’s similar to the question “How to extract text matching a regex using Vim?”. However, unlike the setting in that question, I’m also interested in removing the lines that don’t match, preferably without a hugely complicated regex.

Edit
Report