文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学的各位小编整理了正则表达式-EditPlus 正则表达式替换字符串详解的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
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 “\\”.
例子:
原始串
strabc[991];
strabc[992];
str[11]abc[993];
str[22]abc[994];
str[111]abc[995];
str[222]abc[996];
str[1111]abc[997];
str[2222]abc[999];
目标串:
abc;
abc;
abc[11];
abc[22];
abc[111];
abc[222];
abc[1111];
abc[2222];
处理:
查找串:str\[([0-9]+)\]abc\[[0-9]+\]
替换串:abc[\1]
【1】正则表达式应用——替换指定内容到行尾
原始文本如下面两行
abc aaaaa
123 abc 444
希望每次遇到“abc”,则替换“abc”以及其后到行尾的内容为“abc efg”
即上面的文本最终替换为:
abc efg
123 abc efg
解决:
① 在替换对话框,查找内容里输入“abc.*”
② 同时勾选“正则表达式”复选框,然后点击“全部替换”按钮
其中,符号的含义如下:
“.” =匹配任意字符
“*” =匹配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
功能增强(by jiuk2k):
如果将查找内容“[