learningtech
learningtech

Reputation: 33683

What's wrong with my REGEX while using VI editor?

I have a text document like so:

<table width="10">
</table>

I open the document with the VI editor. I want to replace all instances of width="somenumber" with nothing. I issue this command in the VI editor:

:0,$s/width="[\d]+"//gc

VI says no pattern found. I also tried this and it doens't work:

0,$s/width="[0-9]+"//gc

This one below worked:

:0,$s/width="\d\d"//gc

What's wrong with my first two expressions?

Upvotes: 2

Views: 184

Answers (3)

P Shved
P Shved

Reputation: 99254

You have two errors in your regexp!

First, use \d without []s around it. You probably mix it with character classes like :alpha:, :digit:, etc.

Second, Escape the + sign. By default you should escape it.

So your regexp would be:

:0,$s/width="\d\+"//gc

And, please, read help before posting on stackoverflow:

:h :s

You may also be interested in this help section:

:h magic 

Upvotes: 5

chaos
chaos

Reputation: 124267

You want:

:0,$s/ width="\d\+"//gc

\d isn't recognized inside a character class (or rather, it's recognized as the letter d), and + without a backslash isn't recognized as a metacharacter by vim's BRE. You also probably want the space before width to be eliminated.

Upvotes: 0

John
John

Reputation: 16007

It will only work with widths of two digits, won't it?

Upvotes: 0

Related Questions