editplus regular matching find and replace

Expression Description
\t Tab character.
\n New line.
. Matches any character.
| Matches the characters to the left and right of the expression. For example, "ab|bc" matches "ab" or "bc".
[] Matches part of a list Any single character in . For example, "[ab]" matches "a" or "b". "[0-9]" matches any digit.
[^] Matches any single character outside the list. For example, "[^ ab]" matches characters other than "a" and "b". "[^0-9]" matches any non-numeric character.
The character to the left of * is matched any number of times (0, or more). For example "be *" matches "b", "be" or "bee".
+ The character to its left is matched at least once (1, or more). For example "be+" matches "be" or "bee" but not "b" ".
? The character to the left is matched 0 or 1 times. For example "be?" matches "b" or "be" but not "bee".
The expression to the right of ^ is matched at the beginning of a line. For example " ^A" matches only "A"Beginning of lines.
contentnbsp; The expression to the left of it is matched at the end of a line. For example "econtent"; matches only lines ending with "e".
() affects the order in which expressions are matched and is used as a grouping marker for expressions.
\ Escape character. If you want to use "\" itself, you should use "\\".
Regular expression application - remove blank lines ^[ \t]*\n
Groups of expressions are marked with () . Groups of expressions can be quoted as \0, \1, \2, \3, etc. \0 means all strings to be matched. \1 means the first One grouping, \2 means the second grouping, and so on. Examples are as follows.

Original text find replace result
abc (ab)(c) \0-\1-\2 abc-ab-c
abc a(b)(c) \0-\1-\2 abc-bc
abc (a)b(c ) \0-\1-\2 abc-ac

[1] Regular expression application - replace the specified content to the end of the line
The original text is as follows :
abc aaaaa
123 abc 444

I hope that every time "abc" is encountered, replace "abc" and the content to the end of the line with "abc efg"
, that is, the above text is finally replaced by:
abc efg
123 abc efg

Solution:
① In the Replace dialog box, enter "abc.*" in the Find content
② Check the "Regular Expression" check box at the same time, and then click the "Replace All" button
. The meaning of the symbols is as follows:
"." = matches any character
"*" = match 0 or more times

Note: In fact, it is the replacement of regular expressions. This is just to sort out some of the questions that have been raised. From the regular expression itself, thousands of special cases can be derived.

【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):
如果将查找内容“[0-9][0-9][0-9]”改为“[0-9]*[0-9]”,对应1 或 123 或 12345 或 …
大家根据需要定制

相关内容还有很多,可以自己参考正则表达式的语法仔细研究一下

【3】正则表达式应用——删除每一行行尾的指定字符
因为这几个字符在行中也是出现的,所以肯定不能用简单的替换实现
比如
12345 1265345
2345
需要删除每行末尾的“345”
这个也算正则表达式的用法,其实仔细看正则表达式应该比较简单,不过既然有这个问题提出,说明对正则表达式还得有个认识过程,解决方法如下
解决:
在替换对话框中,启用“正则表达式”复选框
在查找内容里面输入“345contentrdquo;
这里“contentrdquo;表示从行尾匹配

如果从行首匹配,可以用“^”来实现,不过 EditPlus 有另一个功能可以很简单的删除行首的字符串
a. 选择要操作的行
b. 编辑-格式-删除行注释
c. 在弹出对话框里面输入要清除的行首字符,确定

【4】正则表达式应用——替换带有半角括号的多行
几百个网页中都有下面一段代码:
\n
在替换对话框启用“正则表达式”选项,这时就可以完成替换了

【5】正则表达式应用——删除空行
启动EditPlus,打开待处理的文本类型文件。
①、选择“查找”菜单的“替换”命令,弹出文本替换对话框。选中“正则表达式”复选框,表明我们要在查找、替换中使用正则表达式。然后,选中“替换范围”中的“当前文件”,表明对当前文件操作。
②、单击“查找内容”组合框右侧的按钮,出现下拉菜单。
③、下面的操作添加正则表达式,该表达式代表待查找的空行。(技巧提示:空行仅包括空格符、制表符、回车符,且必须以这三个符号之一作为一行的开头,并且以回车符结尾,查找空行的关键是构造代表空行的正则表达式)。
直接在”查找”中输入正则表达式“^[ \t]*\n”,注意\t前有空格符。
(1)选择“从行首开始匹配”,“查找内容”组合框中出现字符“^”,表示待查找字符串必须出现在文本中一行的行首。
(2)选择“字符在范围中”,那么在“^”后会增加一对括号“[]”,当前插入点在括号中。括号在正则表达式中表示,文本中的字符匹配括号中任意一个字符即符合查找条件。
(3)按一下空格键,添加空格符。空格符是空行的一个组成成分。
(4)选择“制表符”,添加代表制表符的“\t”。
(5)移动光标,将当前插入点移到“]”之后,然后选择“匹配 0 次或更多”,该操作会添加星号字符“*”。星号表示,其前面的括号“[]”内的空格符或制表符,在一行中出现0个或多个。
(6)选择“换行符”,插入“\n”,表示回车符。
④、“替换为”组合框保持空,表示删除查找到的内容。单击“替换”按钮逐个行删除空行,或单击“全部替换”按钮删除全部空行(注意:EditPlus有时存在“全部替换”不能一次性完全删除空行的问题,可能是程序BUG,需要多按几次按钮)。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326996277&siteId=291194637