27

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?

DVK
  • 126,886
  • 32
  • 213
  • 327
Scott
  • 10,407
  • 13
  • 37
  • 35

14 Answers14

10

Notepad++ can now handle multi line regular expressions (just update to the latest version - feature was introduced around March '12).

I needed to remove all onmouseout and onmouseover statements from an HTML document and I needed to create a non-greedy multi line match.

onmouseover=.?\s*".*?"

Make sure you check the:

[ ] . matches newline
checkbox if you want to use the multi line match capability.
Mark Salisbury
  • 111
  • 1
  • 3
8

EditPad Pro has better regex capabilities than any other editor I've ever used.

Also, I suspect you have an error in your regex — [\r\n.] will match only carriage returns, newlines, and full stops. If you're trying to match any character (i.e. "dot operator plus CR and LF), try [\s\S] instead.

Ben Blank
  • 54,908
  • 28
  • 127
  • 156
  • @BenBlank Sorry to bring you back to this, ran into this problem. Why doesn't `[\r\n.]` match dot operator plus CR and LF? – Max Burns Mar 14 '13 at 20:33
  • 1
    @Padenton — Most operators don't work inside character classes. For example, `[.()+?*]` matches those characters exactly without any special behavior. Take a look at "Metacharacters Inside Character Classes" [here](http://www.regular-expressions.info/charclass.html) for more info. – Ben Blank Mar 17 '13 at 00:34
5

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
(source: ultraedit.com)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
DVK
  • 126,886
  • 32
  • 213
  • 327
  • 7
    An editor that costs 50 bucks is hardly a replacement for Notepad++. Yeah, it has lots of stuff like FTP client or CSS validation, but what's good in it if I don't need it? – Malcolm Mar 22 '10 at 17:13
  • 4
    @Malcolm - Personally it was worth it for me. Worth it for a LOT of people seeing how the guy(s) making it are still in business :) Also, the OP never asked for a "free replacement". He asked for which editor CAN replace which has a certain feature. – DVK Mar 22 '10 at 17:27
  • I should've said, equivalent replacement. If you have spare 50 dollars, sure, this would be a good investment, but what if I already have everything I need, except for the multiline regex? And also personally I'm not a fan of "do everything with one app", especially in commercial programs, because you pay for everything and use at most half of the featureset. I see you point, though, that's why I didn't vote down or something. It's just my opinion. – Malcolm Mar 22 '10 at 19:08
  • Having actually been forced to use Notepad++ (a new hire can't install UltraEdit yet due to company policy and I was helping her), I must say it lacks WAY too many things I consider basic and incredibly useful-to-necessary in a programming editor/IDE. Sometimes free as in beer is good. Sometimes you get what you pay for :) – DVK Mar 22 '10 at 22:28
  • Mind you, I know some peoples requirements find N++ perfectgly fine. Hell, having programmed in EDLIN back when, I at one time would have found N++ top be manna. – DVK Mar 22 '10 at 22:29
  • Sorry to spoil the fun, but UE has some serious defects in its regex engine especially when multi-line strings are concerned. For example, lookbehind doesn't work across newlines. – Tim Pietzcker May 10 '10 at 07:02
  • @Tim - didn't know that... will remember in the future... do you have a resource with full list of defects?... BTW, now I'm curious... what'd be the practical use of a lookbehind in PCRE in an editor? :) – DVK May 10 '10 at 10:37
  • @DVK: I don't have a list of defects; I just notice time and again that regexes that work fine in EPP or RegexBuddy fail with sometimes curious results in UE. Recent example: http://www.ultraedit.com/forums/viewtopic.php?f=8&t=9417#p33582 (also an example for lookbehind). – Tim Pietzcker May 10 '10 at 10:53
  • I should also mention that I find UE to be a terrific editor. Just the regex engine is not quite as good as I'd like it to be. – Tim Pietzcker May 10 '10 at 20:07
5

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

jussij
  • 10,370
  • 1
  • 33
  • 49
5

I use Eclipse, which is free and that you may already have if you are a developer. '\R' acts as platform independent line delimiter. Here is an example of multi-line search:

search:

\bibitem.(\R.)?\R?{([^{])}$\R^([^\].[^}]$\R.$\R.)

and replace:

\defcitealias{$2}{$3}

Vladtn
  • 2,506
  • 3
  • 27
  • 23
5

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.

Michael
  • 67
  • 1
  • 1
  • 8
    I can't figure out multi-line search with that. – Moss Jul 25 '11 at 02:39
  • Top textbox is what you are looking for. Bottom textbox is what you want to replace it with. Click "find" and then "Replace rest". – GuiSim Oct 13 '11 at 14:42
4

TextPad has good Regex search and replace capabilities; I've used it for a while and am pretty happy with it.

TextPad screenshot

From the Features:

Powerful search/replace engine using UNIX-style regular expressions, with the power of editor macros. Sets of files in a directory tree can be searched, and text can be replaced in all open documents at once.

For more options than you could possibly need, check out "Notepad++ Alternatives" at AlternativeTo.net.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Donut
  • 110,061
  • 20
  • 134
  • 146
2

you can use Python Script plugin for Multiline Regular Expression search and replace! - http://npppythonscript.sourceforge.net/docs/latest/scintilla.html?highlight=pymlreplace#Editor.pymlreplace

# This example replaces any <br/> that is followed by another on the next line (with optional spaces in between), with a single one
editor.pymlreplace(r"<br/>\s*\r\n\s*<br/>", "<br/>\r\n")
dldnh
  • 8,923
  • 3
  • 40
  • 52
pra
  • 21
  • 1
2

I use Notepad++ all the time but it's Regex has alway been a bit lacking.

Sublime Text is what you want.

opticyclic
  • 7,412
  • 12
  • 81
  • 155
1

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

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
0

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 !!!

Valery Letroye
  • 1,035
  • 11
  • 19
0

You could use Visual Studio. Download Express for free if you don't have a copy.

VS's regex is non-standard, so you'd have to use \n:b+[\r\n] instead.

Shane Castle
  • 1,749
  • 2
  • 16
  • 22
  • 1
    Would love a bit more help here. The documentation makes no mention of carriage return (\r) just newline ( http://msdn.microsoft.com/en-us/library/2k3te2cs(v=vs.80).aspx ) I'm trying to find anything that occurs after an opening tag (for resource files) and \[.\n]*foo isn't finding foo unless it's on the same line. – Tom Lianza Feb 11 '11 at 01:36
0

The latest version of UltraEdit has multiline find and replace w/ regex support.

Or if you're OK with using a more specialized regular expression tool for this, there's Regex Hero. It has the side benefit of being able to do everything on the fly. In other words, you don't have to click a button to test your regular expression because it's automatically tested after every keypress.

Personally, I'd use UltraEdit if I'm looking to replace text in multiple files. That way I can just select the files to replace as a batch and click Replace. But if I'm working with a single text file and I'm in need of writing a more complex regular expression then I'd paste it into Regex Hero and work with it there. That's because Regex Hero can save time when you see everything happen in real-time.

Steve Wortham
  • 21,740
  • 5
  • 68
  • 90
  • Actually UltraEdit has had multiline find/replace with Regex for many years. I'm running 8.0 (and just found out I'm 7 releases behind... time to make a petty cash request) and do multiline regex all the time. – jmucchiello Sep 17 '09 at 20:42
  • The new feature I'm referring to is the multiline textboxes in the UltraEdit find/replace dialog box. I'm not sure exactly which version that was introduced in but it's only about a year old I think. Previously they had single line find & replace boxes (the same as most of the other editors). – Steve Wortham Sep 17 '09 at 20:49
0

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.

Blachshma
  • 17,097
  • 4
  • 58
  • 72