.1. 定义和例子 ============ 正则表达式只是一个字符模板,用来在搜索中匹配相同的字符. 在大多数的程序中,正则表达式是括在正斜杠中间的. 例如, /love/就是一个以正斜杠为分隔符的正则表达式,其中的 模板love 将用在搜索所有行中与它匹配的字符. 更为有趣的是 正则表达式可以被非凡的元字符控制.让我们通过下面的例子来进 一步了解这个概念. 有文本如下: Hi tom, I think I failed my anatomy test yesterday. I had a terrible stomach ache. I ate too many fried green tomatoes. Anyway, Tom, Ineed your help. I'd like to make the test up tomorrow, but don't know where to begin studying. Do you think you could help me? After work, about 7 PM, come to my place and I'll treat you to pizza in return for your help. Thanks. Your pal, guy@phantom
--------- 将tom 替换为 David , 使用正则表达式. ------- 替换单词 ------ 从文件的第一行到文件的最后一行.
替换后的文本如下: Hi David, I think I failed my anaDavidy test yesterday, I had terible sDavidach ache. I ate too many fried green Davidatoes. Anyway, David, I need your help. I'd like to make the test up Davidorrow, but don't know where to begin studying. Do you think you could help me? After work, about ? PM, come to my place and I'll treat you to pizza in return for your help. Thanks. Your pal, guy@phanDavid
假设你熟悉 vi 是怎样工作的, 每一个元字符都在 vi 搜索字符串的术语中有具体描述. 下面的例子中, 被加重的字符就是 vi 将找到的匹配字符.
文本如下: 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/
结果如下: 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.