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

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;

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 :)

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

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).
Select your lines.
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).
Select your lines again.
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.

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).

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.

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

Related

Specify anything in brackets - vscode

I have a file that contains texts and inside each text there is a number in parentheses
Is there a way in vscode to select all the numbers in parentheses and delete or replace them in an easy way, I can't do it manually because the scripts exceed 5000
image
You can use the search function in VS Code.
Click on the magnifying glass, in the sidebar.
toggle search details by clicking on the 3 dots in the search pane
name your file in the files to include
enable regular expression
use a regular expression that fits your case, i.e \(\d+\)
enter a replace text (or leave it empty to remove), in the second input and click replace all
You could also use sed to do that.
sed -E 's/\([0-9]+\)/(something else)/g' file.txt > newfile.txt

Replacing the block of code with the new Code

I am having a block of code in Eclipse IDE and I want to replace that with new block of code in the whole project, can any one help me out regarding this issue.
First consider to refactor the repeated code into a function and then you can easily find/replace the function name. It will save you a lot of work.
If its just one line of code, just click ctrl+H select the so it should search in your project, when eclipse finds all results right click on the search window and select "replace all".
If its more than one line of code, enable first regular expression search, so when you select the code and hit ctrl+H eclipse will convert your text to regex, then do the search and select replace all...
(in the search window, make sure to be in the "File Search" tab.)

Find and Replace in Xcode Using Regular Expression

I'm wondering, is there a way that you can do a search for all words that have the word "pPath" in them and replace them with "mutablePath" in Xcode. For instance, there are certain instances where there could be pPath_0 or pPath_50, I just want to replace them all with "mutablePath". Is there a regular expression I can use for this if it's possible?
EDIT #1: I mean to actually replace the whole word pPath_50 or (any word that has pPath in it) with mutablePath. Doing a simple find and replace will only replace the word "pPath" with "mutablePath".
EDIT #2: I found out that if you click the magnifying glass next to the search bar, then click "Show Find Options", you can select "Regular Expression" from a pull down menu and use it to do a search for a regular expression. I'm assuming that the regular expression language used for this is common to other things, does anyone know what the regular expression is to search for any word that has pPath at the beginning of it?
I found that you actually can use regular expressions on find and replace in Xcode 4.3. Simply click on the magnifying glass next to the search box (or the area where you enter in the text to search). Select "Show Find Options", then under "Style" choose "Regular Expression". I'm not too familiar with regular expressions, but I believe it uses syntax that is similar to a lot of other modules that implement them. For instance, I was able to solve my problem using "pPath_\w*".
Yes - Go to Search Navigator (left pane) - Click on the drop down for 'Find' and you will see replace.

Eclipse : search for only a particular word under Eclipse IDE

Is it possible to search for only a particular word under Eclipse IDE .
For example i need to search for a word "sub" .
But the problem is that , when i did ctr l + H and typed the word "sub" , it producing all matching results such as
submit ----etc , but i want the exact word "sub" in my Search .
Please let me know if its possible ??
Thanks in advance .
Try using regular expressions in the File Search Dialogue (Ctrl + H). You can use the word boundaries modifier \b like so :
\bsub\b asks eclipse to search for all matches in which sub is both followed and preceded by a word boundary. Read more about word boundaries here.
Here is a sample snapshot of the Search Results using the above:
If you want to restrict the search to the current project then try selecting 'Enclosing Projects' radio option. This option will be disabled if you don't already have a file from the Project opened. To get past this annoyance, I would recommend creating a Working Set with just the project(s) of interest and then restricting the search on that Working Set.
You can do it from Find/Replace menu (CTRL+F) and flagging the Whole word option :
Otherwise from the standard Search menu(CTRL+H) you can achieve the same result using an appropriate regular expression, in your case you just need to append a space after(or/and before) the sub text and you will get only the whole word.
Select the word and then press Ctrl+Alt+letter(G) it will search the word where it is used.

How to replace a string in eclipse?

I have 500+ program files in a project. I would like to replace a string "vector" to "std::vector" in all the files. How can I do this in Eclipse? I am using Eclipse CDT Helios.
(One option is to use Search->Search and enter the string to be replaced and press "Replace" button. But the problem here is that, it will replace "vectorOfPoints" and "pointsVector" also.)
I guess you could just search for ' vector ' and replace it with ' std::vector ' as you described yourself. Instead make sure there is a space leading and ending your search, so you take only the single word vector.
Maybe you'll only need a space in the end, if vector occurs at the start of a new line. Also make sure your search is case sensitive.
By using Search -> File... from the menu you can search for strings in any files. As Containing text you should use a regular expression so that vectorOfPoints will not match. Then click on Replace... and Eclipse will look up all occurrences. Here you can even have a preview before executing the replace operation.
You can use Edit->Find/Replace and from options select whole world so it will not replace vectorOfPoints