8. 示例
- 基本正则表达式
• 首先打开vim编辑器, 输入下面的一段文本:
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love
is forever. I live for you. It's hard to get back in the
groove.
• 作如下测试:
/love 查找love
/^love 查找行首的love
/love$ 查找行尾的love
/l.ve 查找l后面跟任意字符再跟ve字符
/o*ve 零个或多个o后面跟ve
10. 示例
-元字符组合
• 打开vim编辑器输入如下内容:
Christian Scott lives here and will put on a Christmas party.
There are around 30 to 35 people invited.
They are:
Tom
Dan
Rhonda Savage
Nicky and Kimberly.
Steve, Suzanne, Ginger and Larry.
• 作如下测试, 并查看结果:
/^[A-Z]..$
/^[A-Z][a-z ]*3[0-5]
/[a-z]*.
/^ *[A-Z][a-z][a-z]$
/^[A-Za-z]*[^,][A-Za-z]*$
11. 示例
- 其他元字符
• 打开vim编辑器输入以下文本:
Unusual occurences happened at the fair.
Patty won fourth place in the 50 yard dash square and fair.
Occurences like this are rare.
The winning ticket is 55222.
The ticket I got is 54333 and Dee got 55544.
Guy fell down while running around the south bend in his last event.
• 作如下测试:
/<fourth> # 查找单词fourth
/<f.*th>/ # 查找f开头跟0个或多个任意字符以th结尾的
任意单词或者词组
# 将Occurence或者occurence分别替换为Occurrence和
occurrence
:1,$s/([Oo]ccur)ence/1rence/
# 将当前行的square and fair替换为fair and square
:s/(square) and (fair)/2 and 1