How to search and replace 2 lines (together) in Eclipse?

Solution 1:

Another tip on how to get the regex for a selected block. 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. Now in the Find text box you have the regular expression that exactly matches your selection block.

(I discovered this, only after creating manually a regexp for very long block :)

Solution 2:

Search are multi-line by default in Eclipse when you are using regex:

(\@Length\(max = L_255)\)([\r\n\s]+private)

I would like to add "private String description;"

(\@Length\(max = L_255)\)([\r\n\s]+private\s+?String\s+description\s*?;)

replaced by:

\1, message="{validator.description.len}")\2

It works perfectly in a File Search triggered by a CTRL-H.

Eclipse multi-line search

As mentioned in Tika's answer, you can directly copy the two lines selected in the "Containing Text" field: those lines will be converted as a regexp for you by Eclipse.