tags:

views:

71

answers:

3

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?

+1  A: 

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.

chaos
I just tried this and it says no pattern found 0,$s/width="[0-9]+"//gc
John
How about just removing the square brackets?
UncleO
:0,$s/width="\d+"//gc
UncleO
A: 

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

John at CashCommons
+5  A: 

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
Pavel Shved