21

Eclipse regexp search works pretty well, so for example in search box I have this:

(?s)(myMethod.*?;)\}\);

Now I want to copy multiline text in the IDE and in replace box, for example I want to paste \1PASTE_MULTILINE_TEXT_HERE. However Eclipse does not allow me to directly copy-paste multiline text without manually inserting newline characters.

In Vim (Gvim, Macvim) it works perfectly well, keeping all the spaces; how can I do the same thing in Eclipse?

Lii
  • 11,553
  • 8
  • 64
  • 88
codegood
  • 211
  • 1
  • 2
  • 4
  • http://stackoverflow.com/questions/4154239/java-regex-replaceall-multiline – Sean F May 23 '13 at 03:35
  • 1
    thanks, but that is more about multiline search, enabled by dotall flag or (?s) which I already do. Please note that search part is working, multiline replace is not – codegood May 23 '13 at 06:56
  • @codegood after some extensive research I found out that you can use the file based search boxes to do multi-line replace. The fact that the "editor" replace (in the find popup box) fails without any indication I consider an Eclipse bug and I'll have to find out if I have to create a bug report for it... – Maarten Bodewes Jun 06 '13 at 12:41
  • @MaartenBodewes - have you created or found a bug report for this? – Daniel Sokolowski Apr 08 '15 at 02:55
  • @DanielSokolowski I cannot fully remember if I had the time to research this any further. I certainly did not file a bug report. – Maarten Bodewes Apr 08 '15 at 07:33

2 Answers2

35

For searching multiple lines in Eclipse, you must use the 's' parameter in search expression:

(?s)someExpressionToMatchInAnyLine

For replacing with multiple lines exp you must use \R i.e:

line1\Rline2\Rline3

This will replace the matched exp with:
line1
line2
line3

Fabrice TIERCELIN
  • 911
  • 11
  • 11
Eugenio
  • 563
  • 5
  • 8
  • 1
    Additional information: `?s` is a feature of the [Java regex implementation](https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#DOTALL) and is called an "embedded flag expression". I strongly suspect that the other flag expressions also work in the Eclipse search dialog. – Lii Mar 01 '19 at 12:44
4

Generally, the approach I've taken to doing this sort of thing is to type out what I want to use as a replacement, select that, open up the Find/Replace dialog, and copy the contents of the Find text box. I proceed from there and paste what I copied into the Replace text box. There is still a little work to be done (removing backslashes from in front of regex special characters that don't apply in the Replace box), but it gives me a hand up.

Paul Rowe
  • 778
  • 3
  • 10