

EditPlus 正则表达式替换字符串详解
我用过的EP表达式
删除重复的行:编辑-删除-删除重复行
删除以@HK开头的行:替换^@HK.*为空
删除含有“散客线路”的行中从“散客线路”开始到行末的内容:正则替换“散客线路.*”为空即可,注:这里的查找里不需要行末正则符
在行尾插入分号:替换\n为;\n
在含有字符串reg delete的行尾加上引号:替换(^reg delete.*)为\1"
例:在下段要每行数字后加上“[/url]”
南宁、北海银滩一日游
行程地址:[url]http://8y8z.com/line/line_viewkh.asp?prono=1071
南宁、北海银滩二日游
行程地址:[url]http://8y8z.com/line/line_viewkh.asp?prono=1067
方法:
EP正则替换“prono=[0-9][0-9][0-9][0-9]”为“\0\1\2\3[/url]”或者替换“[0-9][0-9][0-9][0-9]”为“\0\1\2\3[/url]”
如最后的数字不都是四数的时,替换prono=[0-9]*[0-9]为\0\1[/url]
把“<a”和“">之间的内容替换为http://:正则替换“<a.*">”为“http://”
EditPlus的查找,替换,文件中查找支持以下的正则表达式
Expression Description(表达式字符的英文说明)
\t Tab character.
\n New line.
. Matches any character.
| Either expression on its left and right side matches the target string.
For example, “a|b” matches “a” and “b”.
[] Any of the enclosed characters may match the target character.
For example, “[ab]” matches “a” and “b”. “[0-9]” matches any digit.
[^] None of the enclosed characters may match the target character.
For example, “[^ab]” matches all character EXCEPT “a” and “b”.
“[^0-9]” matches any non-digit character.
* Character to the left of asterisk in the expression should match 0 or more times.
For example “be*” matches “b”, “be” and “bee”.
+ Character to the left of plus sign in the expression should match 1 or more times.
For example “be+” matches “be” and “bee” but not “b”.
? Character to the left of question mark in the expression should match 0 or 1 time.
For example “be?” matches “b” and “be” but not “bee”.
^ Expression to the right of ^ matches only when it is at the beginning of line.
For example “^A” matches an “A” that is only at the beginning of line.
$ Expression to the left of $ matches only when it is at the end of line.
For example “e$” matches an “e” that is only at the end of line.
() Affects evaluation order of expression and also used for tagged expression.
\ scape character. If you want to use character “\” itself, you should use “\\”.
例子:
原始串
str[1]abc[991];
str[22]abc[994];
str[111]abc[995];
str[2222]abc[999];
目标串:
abc[1];
abc[22];
abc[111];
abc[2222];
处理:
查找:str[([0-9]+)]abc[[0-9]+] 替换成:abc[1]
【1】正则表达式应用——替换指定内容到行尾
如下两行
abc aaaaa
123 abc 444
替换为:
abc efg
123 abc efg
替换行中以“abc”开始到行尾的内容为“abc efg”
解决:
替换“abc.*”为“abc efg”,要勾选“正则表达式”复选框,再点“全部替换”
“.”表匹配任意字符
“*” 表匹配0次或更多
注意:正则表达式可引申出成千上万种特例。
【2】正则表达式应用——数字替换
替换
asdadas123asdasdas456asdasdasd789asdasd
为:
asdadas[123]asdasdas[456]asdasdasd[789]asdasd
查找“[0-9][0-9][0-9]”替换为:“[\0\1\2]”,不含引号
说明:
“[0-9]”表匹配0~9间任何特例,同样“[a-z]”表匹配a~z间任何特例,重复用“[0-9]”,表连续出现的三个数字
“\0”代表第一个“[0-9]”对应的原型,“\1”代表第二个“[0-9]”对应的原型,依此类推!
这里查找和替换中的大括号意思不同,前者大括号为表达式“[]”表范围内任意字符后者为单纯的字符,表添加“[”或“]”,如输入“其它\0\1\2其它”。
则替换结果为:
asdadas其它123其它asdasdas其它456其它asdasdasd其它789其它asdasd
功能增强:
如果将查找内容“[0-9][0-9][0-9]”改为“[0-9]*[0-9]”,对应1 或 123 或 12345 或 …
【3】正则表达式应用——删除每一行行尾的指定字符
如
12345 1265345
2345
删除每行末“345”
解决:
查找“345$”替换为空
说明:
“$”表示从行尾,从行首匹配,用“^”实现,
其它删除行首的字符串
a. 选择要操作的行,编辑-格式-删除行注释,输入要清除的行首字符,确定
【5】正则表达式应用——删除空行
技巧提示:空行仅包括空格符、制表符、回车符,且必须以这三个符号之一作为一行的开头,并且以回车符结尾,找空行关键是构造空行的正则表达式)。
查找中输入“^[ \t]*\n”,注:\t前有空格符。
(1)“^” 表示待查找字符串必须出现在文本中一行的行首。
(2)“[]” 中括号在正则中表要查找的文本中字符匹配括号中任意一字符即符合条件。
(3)空格符是空行的一个组成成分。
(4)添加代表制表符的“\t”。
(5)“*” 星号表其前面的括号“[]”内的空格符或制表符在行中“匹配 0 次或更多”。
(6)插入“\n”,表示回车符。
注:EditPlus有时存在“全部替换”不能一次性完全删除空行的问题,要多按几次)。
其它正则替换例子:
1.
“Error adding the post!”;
“Error adding the comment!”;
“Error adding the user!”;
查找:
“Error adding ([^!|"|;]*)
替换成:
“在增加\1时发生错误
解释:([^!|"|;]*) 的意思是 不等于 ! 和 ” 和 ; 中的任何一个,意思就是这3个字符之外的所有字符将被选中(替换区域);
\1 即被选中的替换区域所在的新位置(复制到这个新位置)。
替换后结果是:
Code:
“在增加the post时发生错误!”;
“在增加the comment时发生错误!”;
“在增加the user时发生错误!”;
再替换一次把the post、the comment、the user替换成你要翻译的词。
最后结果:
Code:
“在增加帖子时发生错误!”;
“在增加评论时发生错误!”;
“在增加用户时发生错误!”;
2.要提取的单词在中间,如:
can not be deleted because
can not be added because
can not be updating because
查找原文件:
can not be ([^ ]*) because
替换成:
无法被\1因为
替换后结果是:
Code:
无法被deleted因为
无法被added因为
无法被updating因为
其余步骤如上。
3.如下:
abandon[2'b9nd2n]v.抛弃,放弃
abandonment[2'b9nd2nm2nt]n.放弃
abbreviation[2bri:vi'ei62n]n.缩写
abeyance[2'bei2ns]n.缓办,中止
abide[2'baid]v.遵守
ability[2'biliti]n.能力
able['eibl]adj.有能力的,能干的
abnormal[9b'n0:m2l]adj.反常的,变态的
aboard[2'b0:d]adv.船(车)上
1.
查找: (^[a-zA-Z0-0\-]+)(\[*.*\]+)(.*)
替换为: @@@@@”\1″,”\2″,”\3″,
效果:
@@@@@”abandon”,”[2'b9nd2n]“,”v.抛弃,放弃”,
@@@@@”abandonment”,”[2'b9nd2nm2nt]“,”n.放弃”,
@@@@@”abbreviation”,”[2bri:vi'ei62n]“,”n.缩写”,
@@@@@”abeyance”,”[2'bei2ns]“,”n.缓办,中止”,
@@@@@”abide”,”[2'baid]“,”v.遵守”,
@@@@@”ability”,”[2'biliti]“,”n.能力”,
@@@@@”able”,”['eibl]“,”adj.有能力的,能干的”,
@@@@@”abnormal”,”[9b'n0:m2l]“,”adj.反常的,变态的”,
@@@@@”aboard”,”[2'b0:d]“,”adv.船(车)上”,
2.删除所有换行变成一段!
查找: \n替换空
效果:
@@@@@”abandon”,”[2'b9nd2n]“,”v.抛弃,放弃”,@@@@@”abandonment”,”[2'b9nd2nm2nt]“,”n.放弃”,@@@@@”abbreviation”,”[2bri:vi'ei62n]“,”n.缩写”,@@@@@”abeyance”,”[2'bei2ns]“,”n.缓办,中止”,@@@@@”abide”,”[2'baid]“,”v.遵守”,@@@@@”ability”,”[2'biliti]“,”n.能力”,@@@@@”able”,”['eibl]“,”adj.有能力的,能干的”,@@@@@”abnormal”,”[9b'n0:m2l]“,”adj.反常的,变态的”,@@@@@”aboard”,”[2'b0:d]“,”adv.船(车)上”,@@@@@”abolish”,”[2'b0li6]“,”v.废除,取消”,@@@@@”abolition”,”[9b2'li62n]“,”n.废除,取消”
3.在@@@@@处加上换行并删除@@@@@
查找: @@@@@替换为: \n
效果:
“abandon”,”[2'b9nd2n]“,”v.抛弃,放弃”,
“abandonment”,”[2'b9nd2nm2nt]“,”n.放弃”,
“abbreviation”,”[2bri:vi'ei62n]“,”n.缩写”,
“abeyance”,”[2'bei2ns]“,”n.缓办,中止”,
“abide”,”[2'baid]“,”v.遵守”,
“ability”,”[2'biliti]“,”n.能力”,
“able”,”['eibl]“,”adj.有能力的,能干的”,
“abnormal”,”[9b'n0:m2l]“,”adj.反常的,变态的”,
“aboard”,”[2'b0:d]“,”adv.船(车)上”,
“abolish”,”[2'b0li6]“,”v.废除,取消”,