日常經(jīng)常使用notepad++進(jìn)行文本處理有序,因?yàn)閚otepad++的文本查找和替換支持正則表達(dá)式,使用起來(lái)非常方便岛请。查找的界面如下所示:
notepad++的文本查找界面
【查找目標(biāo)】支持多種查找模式來(lái)查找文本文件旭寿,其中的正則表達(dá)式模式大大增強(qiáng)了文本查找的能力。進(jìn)而我們可以用【替換為】輸入框中的文本崇败,去替換查找出來(lái)的文本盅称。但如果我們?cè)凇咎鎿Q為】輸入框中想要使用【查找目標(biāo)】中匹配到的字段,那該怎么做呢后室?
我們可以使用正則表達(dá)式的分組能力缩膝,在【查找目標(biāo)】中的正則表達(dá)式通過(guò)()來(lái)包圍想要用的字符,然后在【替換為】輸入框中用\1岸霹、\2疾层、…來(lái)獲取查找到的第一、第二松申、… 個(gè)分組云芦。
比如:
Text body | Search string | Replace string | Result |
---|---|---|---|
Hi my name is Fred | my name is (.+) | my name is not \1 | Hi my name is not Fred |
The quick brown fox jumped over the fat lazy dog | brown (.+) jumped over the (.+) | brown \2 jumped over the \1 | The quick brown fat jumped over the fox lazy dog |
這是一種常見(jiàn)的場(chǎng)景,比如下面的需求贸桶,假設(shè)原始的文本文件是這樣:
iconUrl = in.readString();
title = in.readString();
subTitle = in.readString();
description = in.readString();
imageUrl = in.readString();
imageJumpSceheme = in.readString();
buttonText = in.readString();
buttonJumpScheme = in.readString();
針對(duì)每一行舅逸,我想把它換成this.xxx = xxx
的形式,這里每一行的xxx都是不一樣的皇筛,沒(méi)法簡(jiǎn)單匹配替換琉历,那么可以使用分組的方法:
【查找目標(biāo)】:(.*) = in.readString\(\);
【替換為】:info.\1 = \1;
結(jié)果如下:
info.iconUrl = iconUrl;
info.title = title;
info.subTitle = subTitle;
info.description = description;
info.imageUrl = imageUrl;
info.imageJumpSceheme = imageJumpSceheme;
info.buttonText = buttonText;
info.buttonJumpScheme = buttonJumpScheme;