Multiline Regular Expression search and replace!

2019-01-18 08:34发布

I've hit a wall. Does anybody know a good text editor that has search and replace like Notepad++ but can also do multi-line regex search and replace? Basically, I am trying to find something that can match a regex like:

search oldlog\(.*\n\s+([\r\n.]*)\);
replace newlog\(\1\)

Any ideas?

14条回答
▲ chillily
2楼-- · 2019-01-18 09:27

ED for windows has two versions of regex, three sorts of cut and paste (selection, lines or blocks, AND you can shift from one to the next (unlike ultra edit, which is clunky at best) with just a mouse click while you are highlighting -- no need to pull down a menu. The sheer speed of getting the job done is incredible, like reading on a Kindle, you don't have to think about it.

查看更多
看我几分像从前
3楼-- · 2019-01-18 09:29

I'm pretty sure Notepad++ can do that now via the TextFX plugin (which is included by default). Hit Control-R in Notepad++ and have a play.

查看更多
Bombasti
4楼-- · 2019-01-18 09:32

You can use a recent version of Notepad++ (Mine is 6.2.2).

No need to use the option ". match newline" as suggested in another answer. Instead, use the adequate regular expression with ^ for "begin of line" and $ for "end of line". Then use \r\n after the $ for a "new line" in a dos file (or just \n in a unix file as the carriage return is mainly used for dos/windows text file):

Ex.: to remove all lines starting with tags OBJE after a line starting with a tag UID (from a gedcom file - used in genealogy), I did use the following search regex:

^UID (.*)$\r\n^(OBJE (.*)$\r\n)+

And the following replace value:

UID \1\r\n

This is matching lines like this:

UID 4FBB852FB485B2A64DE276675D57A1BA
OBJE @M4@
OBJE @M3@
OBJE @M2@
OBJE @M1@

and the output of the replacement is

UID 4FBB852FB485B2A64DE276675D57A1BA

550 instances have been replaced in less than 1 sec. Notepad++ is really efficient!

Otherwise, to validate a Regular expression I like to use the .Net RegEx Tester (http://regexhero.net/tester/). It's really great to write and test on the fly a Reg Ex...

PS.: Also, you can use [\s\S] in your regex to match any character including new lines. So, if you look for any block of "multi-line" text starting with "xxx" and ending with "abc", the following Regex will be fine:^xxx[\s\S]*?abc$ where "*?" is to match as less as possible between xxx and abc !!!

查看更多
Emotional °昔
5楼-- · 2019-01-18 09:33

My personal recommendation is IDM Computing's UltraEdit (www.ultraedit.com) - it can do regular expressions (both search and replace) with Perl, Unix and UltraEdit syntax. Multi-line matching is one of the capabilities in Perl regex mode in it.

It also has other nice search capabilities (e.g search in specific character column range, search in multiple files, search history, search favorites, etc...)

alt text http://www.ultraedit.com/assets/images/new_features/ue_1500/searchincolumn.png

查看更多
forever°为你锁心
6楼-- · 2019-01-18 09:33

EditPlus does a good job at search/replace using regex (including multiline)

查看更多
太酷不给撩
7楼-- · 2019-01-18 09:35

The Zeus editor can do multi-line search and replace.

查看更多
登录 后发表回答