.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.