Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline search replace with regexp in Eclipse

Tags:

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?

like image 754
codegood Avatar asked May 23 '13 02:05

codegood


People also ask

How to replace multiple lines in eclipse?

Open one of the files that contains the multiple lines (multiline) to search or replace. Click Ctrl + F and select "Regular expression". Close the Find/Replace window. Select the block you need and click again Ctrl + F to open the Find/Replace window.

What is multiline in regex?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.

What is multiline flag in regex?

The m flag indicates that a multiline input string should be treated as multiple lines. For example, if m is used, ^ and $ change from matching at only the start or end of the entire string to the start or end of any line within the string. You cannot change this property directly.


2 Answers

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

like image 120
Eugenio Avatar answered Oct 12 '22 07:10

Eugenio


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.

like image 34
Paul Rowe Avatar answered Oct 12 '22 06:10

Paul Rowe