[wellylug] vi(m) question.

Sam Cannell sam at plaz.net.nz
Sat May 8 12:16:53 NZST 2004


The first thing to note is that regular expressions in Vim seem to need
more backslashes than pretty much everything else.  I'm not sure if this
is just a difference between Posix and Perl expressions, or if it's
something different again.

The parentheses mark stuff you want to keep in the new string.  The
first set of parens can be retrieved with \1, the second with \2, etc
etc.

Adding a + after a character means 'one or more of the previous
character'.  So 'a+' would match 'a', 'aaaa', 'aaaaaaaaaaa', etc.

Adding a * means '0 or more of the character', so 'a*' matches 'aaaaa',
'aaaaaaaaa', and ''.

Other special characters are:

^ - the beginning of a string
$ - the end of a string
. - any character at all

You can use square brackets to match any one of a set of characters.
For example, '[aeiou]' would match any lowercase vowel.

If you wanted to remove every lowercase vowel from a file, you would do
something like:

:%s/[aeiou]//g

The g at the end allows the expression to match more than once on any
given line.  The following:

:%s/[aeiou]//

Would only match the first on each line.

To match everything *except* lowercase vowels, you add a ^ as the first
character inside the square brackets.  When it's given as the first
character in the [] set, it doesn't mean the beginning of the line as
mentioned above.  The following:

:%s/[^aeiou]//g

Would remove everything that is not a lowercase vowel from the file.

There are heaps of other far more complex things you can do.  For
example, the following expression will match any valid date in
mm/dd/yyyy or dd/mm/yyyy format, including testing for leap years:

((^(10|12|0?[13578])([/])(3[01]|[12][0-9]|0?[1-9])([/])((1[8
-9]\d{2})|([2-9]\d{3}))$)|(^(11|0?[469])([/])(30|[12][0-9]|0
?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(2[0-
8]|1[0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2
)([/])(29)([/])([2468][048]00)$)|(^(0?2)([/])(29)([/])([3579
][26]00)$)|(^(0?2)([/])(29)([/])([1][89][0][48])$)|(^(0?2)([
/])(29)([/])([2-9][0-9][0][48])$)|(^(0?2)([/])(29)([/])([1][
89][2468][048])$)|(^(0?2)([/])(29)([/])([2-9][0-9][2468][048
])$)|(^(0?2)([/])(29)([/])([1][89][13579][26])$)|(^(0?2)([/]
)(29)([/])([2-9][0-9][13579][26])$))

(stolen from http://regexlib.com/REDetails.aspx?regexp_id=279)

The regex(7) manpage has a lot more information, but is very hard to
understand. :)

-----Original Message-----
From: wellylug-admin at lists.naos.co.nz
[mailto:wellylug-admin at lists.naos.co.nz] On Behalf Of Enkidu
Sent: Saturday, 8 May 2004 11:22 a.m.
To: wellylug at lists.naos.co.nz
Subject: Re: [wellylug] vi(m) question.

On Fri, 7 May 2004 23:57:20 +1200, you wrote:

I guess that "\1" in the second half means "everything in the ( ) from
the first part". 

The first bit means "at the start of the string remember everything
including the white space but what does "\+\" mean?





More information about the wellylug mailing list