'Remove all occurence of _[number].htm in vim

I've a file with occurence in the form _[number].htm, for example _43672151820.htm

How I can remove all occurrences of strings with a matching pattern?



Solution 1:[1]

Substitute substring

Use this regular expression with the substitution command %s

:%s/_\d\+\.htm//g

Explanation (from regex101.com):

_ matches the character _ with index 9510 (5F16 or 1378) literally (case sensitive)
\d matches a digit (equivalent to [0-9])
\+ matches the character + with index 4310 (2B16 or 538) literally (case sensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
htm matches the characters htm literally (case sensitive)
Global pattern flags 
g modifier: global. All matches (don't return after first match)

Substitute word

The above regular expression will match for instance 123.htm in ab_123.htm. If you want to match a word use vim's word boundaries\< and \>:

:%s/\<_\d\+\.htm\>//g

(see In Vim, how do you search for a word boundary character, like the \b in regexp?)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1