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

EclipseEditorReplace

Eclipse Problem Overview


I would like to search multiple files via eclipse for the following 2 lines:

@Length(max = L_255)
private String description;

and replace them with these two:

@Length(max = L_255, message="{validator.description.len}")
private String description;

Eclipse Solutions


Solution 1 - Eclipse

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

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.

Solution 3 - Eclipse

CTRL+H does take two lines if you use regexp (and you don't have to write the regexp by yourself, eclipse does that for you).

  1. Select your lines.
  2. Click CTRL+H. The search dialog opens up. If "Regular expression" is already checked, eclipse will have converted the two lines you search for into regexp for you, click Search. If "Regular expression" if not already checked", check it and click Cancel (eclipse remembers your choice).
  3. Select your lines again.
  4. Click CTRL+H. The search dialog opens up. This time "Regular expression" is already selected. eclipse will have converted the two lines you search for into regexp for you, click Search.

Solution 4 - Eclipse

A quick tip for including multiple lines as part of a manually constructed regular expression:

Where you would normally use .* to match any character zero or more times, instead consider using something like (?:.|\r?\n)*. Or put an extra ? at the end to make it non-greedy.

Explanation: . doesn't match new lines so need to do an "either-or": The parentheses match either the . before the pipe or the new line after it. The ? after \r makes the carriage return before the line feed optional to allow Windows or Unix new lines. The ?: excludes the whole thing as a capturing group (which helps to avoid a stack overflow).

Solution 5 - Eclipse

Click Ctrl + F and select "Regular Expression" and then search the lines. In case to perform the same on multiple files, click Ctrl + H, click on 'File Search' and perform the same.

Solution 6 - Eclipse

Select the folder that contains all your files and press Ctrl+H.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamView Question on Stackoverflow
Solution 1 - EclipseMario BalabanView Answer on Stackoverflow
Solution 2 - EclipseVonCView Answer on Stackoverflow
Solution 3 - EclipseTikaView Answer on Stackoverflow
Solution 4 - EclipseSteve ChambersView Answer on Stackoverflow
Solution 5 - EclipsePriya KadamView Answer on Stackoverflow
Solution 6 - EclipseBart van HeukelomView Answer on Stackoverflow