Removing 1000s of comments in eclipse? - eclipse

I installed JD-GUI to retrieve my code from a jar file. Everything works fine, except JD-GUI automatically adds annoying comments like this:
Any way I can remove them? I don't understand regex.

Using Eclipse:
Go to Edit > Find/Replace...
Use this regular expression in the Find box: ^/\* [0-9 ]{3} \*/
^ match start of line.
/\* match start of comment
[0-9 ]{3} match exactly three digits/spaces
\*/ match end of comment
Make sure the Replace box is empty.
Make sure the Regular expressions checkbox is selected.
Click Replace All

Use CTRL+H. Within "File Search" > "Search string", check "Regular expression" and use one of the regex given by the other answers.
Then use "Replace..." to replace them all with nothing.

Use the utility sed to search for a regex and replace with an empty string. Here is a gist that should get you started with using it.
Since you don't understand regex, I'll help you out with it: /^\/\* \d+ \*\//gm will find every comment block that starts at the beginning of a line and contains a line number.
Here's how it works:
/ is the start of the regex
^ matches the begnning of the line
\/\* finds the opening /* of the comment
(space) finds the space before the line number
\d+ finds any number of digits
(space) finds the space after the line number
\*\/ finds the ending */ of the comment
/gm ends the regex and flags this as a global, multiline search

Related

Find and replace all occurrences not in quotes

I'm in the middle of refactoring my Python code to use underscore notation as described in PEP8 and I am using VS Code's find-and-replace tool. The problem is occurrences of words in quotes are also being replaced. How can I replace all occurrences that aren't in quotes?
Example:
fooBar = df["fooBar"]
Desired after find and replace
foo_bar = df["fooBar"]
VS Code's Search functions supports regex, and you can use a negative lookbehind pattern to find "all fooBar strings that are not preceded by a quote": (?<!")fooBar.
(1) enables regex expressions when searching. As you can see, that ignores all the "fooBar" instances. Then you can also turn on (2) so that it enables whole word matching (so that it doesn't match "fooBar123").
As a side note, I hope you have automated tests you can run after doing all these renaming :)
open the find-and-replace vscode tool(ctrl+f and ctrl+h)
select the User Regular Expression(Alt+R)
select the Match Case (Alt+c)
Find = ([a-z]*)(?<!_)([A-Z])(?<!".*)
Replace = $1_\L$2
finaly press Enter
result is :

Multiline regular expression search in Visual Studio Code

Multiline regular expression search doesn't work in VS Code version 1.27.2 .
Theoretically aaa(\n|.)*bbb should find string starting from aaa and ending bbb but it doesn't work.
The solution mentioned here Multi-line regular expressions in Visual Studio Code doesn't work as well.
Multiline search is added in v1.29 released in November 2018. See multi-line search.
VS Code now supports multiline search! Same as in the editor, a regex
search executes in multiline mode only if it contains a \n literal.
The Search view shows a hint next to each multiline match, with the
number of additional match lines.
This feature is possible thanks to the work done in the ripgrep tool
to implement multiline search.
Multiline search is coming to the Find Widget with v1.38. See multiline find "pre-release" notes.
Multi Line search in Find Widget
The Find Widget now supports multiple line text search and replace. By
pressing Ctrl+Enter, you can insert new lines into the input box.
.
Odd that it is Ctrl+Enter in the Find Widget but Shift+Enter in the Search Panel (see Deepu's answer below). Shift+Enter has other functionality when the Find Widget is focused.
yes, you could use regex for mutliple line search in VScode.
To find a multi-line text block starting from aaa and ending with the first bbb (lazy qualifier)
aaa(.|\n)+?bbb
To find a multi-line text block starting from aaa and ending with the last bbb. (greedy qualifier)
aaa(.|\n)+bbb
I have been looking for a quick way to do this, and I have come to the following:
start_text.*?(.|[\n])*?end_text
with start_text and end_text being the bounds of your multiline search.
breaking down the regex ".?(.|[\n])?":
".?" will match any characters from your start text to the end of the line. The "?" is there to ensure that if your end_text is on the same line the . wont just keep going to the end of the line regardless (greedy vs lazy matching)
"(.|[\n])" means either a character\whitespace or a new line
"*?" specifies to match 0 or more of the expression in the parentheses without being greedy.
Examples:
<meta.*?(.|[\n])*?/> will match from the beginning of all meta tags to the end of the respective tags
<script.*?(.|[\n])*?</script> will match from the beginning of all script tags to the respective closing tags
Warning:
Using .*?(.|[\n])*? with improperly or partially filled in start_text or end_text might crash VS Code. I suggest either writing the whole expression out (which doesn't cause a problem) or writing the start and end text before pasting in the regex. In any case, when I tried to delete parts of the starting and ending text VS Code froze and forced me to reload the file. That being said, I honestly could not find something that worked better in VS Code.
Without using regex.
Multi-line search is now possible in vs code version 1.30 and above without using regex.
Type Shift+Enter in the search box to insert a newline, and the search box will grow to show your full multiline query. You can also copy and paste a multiline selection from the editor into the search box.
You can find and replace in multiple lines by using this simple regex : StringStart\r\nStringEnd
For example
public string MethodA(int x)
{
var user;
}
public string MethodB(string y)
{
var user;
}
public string MethodC(int x)
{
var user;
}
public string MethodD(float x)
{
var user;
}
If you want to replace the name of user variable with customer along with method parameter name to user but only for the int ones.
Then the regex to find will be : int x)\r\nEnterBlankSpacesHereToReachTheString{\r\nEnterBlankSpacesHereToReachTheStringvar user
and regex to replace will be : int user)\r\nEnterBlankSpacesHereToReachTheString{\r\nEnterBlankSpacesHereToReachTheStringvar customer
See for reference
I had a similar issue, this works better for me:
aaa[.\n\r\t\S\s]*bbb
This includes carriage return (\r), new line (\n), tab (\t), any whitespece (\s) and any non whitespace (\S). There seems to be some redundancy putting "." and "\S" together, but it doesn't work without both in my case.
No regex way: you can copy multiline text and paste it in "Find in files" form:
result of "Replace all":
(.|\n)+? or [\s\S\r]* or [.\n\r\t\S\s]* may be understandable when viewed in isolation, but in an already complex regex expression, they can add that extra layer of complexity that makes the whole thing unmanageable.
On Windows, for files on the local disk, I find the best solution is to switch to using Notepad++. Not only does it handle multi-line out of the box, it also has a pleasant interface for multi-file search and replace, handles macros gracefully, and is quite light-weight. You can switch back to VScode as soon you have finished your regex changes. Personally, I deleted Notepad++ when I found VScode, but reinstalled it later when I found some of what Notepad++ had to offer was missing in VScode. Both are free to use! I'm sure there's an equivalent on the Mac.
If you are willing to search JavaScript, TypeScript or JSON files I can recommend my VScode extension
It allows for formatting agnostic text search and structural code search
You can find it on codeque.co or at VSCode Marketplace
Your query could look like this
aaa$$mbbb
where $$m means optional multiline set of any characters
Make sure to use text mode for this query
CodeQue can make much more than that!
The reason on this behavior is very simple.
Multiple line search isn't implemented yet.
see: Support multi-line search for Global search

How do you delete lines with certain keywords in VScode

I have this regular expression to find certain keywords on a line:
.*(word1|word2|word3).*
In the find and replace feature of the latest VSCode it works ok and finds the words but it just blanks the lines leaving big gaps in-between.
I would like to delete the entire line including linefeed.
The find and replace feature doesnt seem to support reg exp in the replace field.
If you want to delete the entire line make your regex find the entire line and include the linefeed as well. Something like:
^.*(word1|word2|word3).*\n?
Then ALT-Enter will select all lines that match and Delete will eliminate them including the lines they occupied.

Notepad++ newline in regex

Suppose you have this file:
x
a
b
c
x
x
a
b
c
x
x
and you want to find the sequence abc (and select the whole 3 lines) with Notepad++ . How to express the newline in regex, please?
Notepad++ can do that comfortably, you don't even need regexes
In the find dialogue box look in the bottom left and switch your search mode to Extended which allows \n etc.
As odds on you're working on a file in windows format you'll be looking for \r\n (carriage return, newline)
a\r\nb\r\nc
Will find the pattern over three lines
Update 18th June 2012
With the new Notepad++ v6, you can indeed search for newlines with regexes. So you can just use
a\r\nb\r\nc
even with regular expressions to accomplish what you want. Note \r\n is Windows encoding of line-breaks. In Unix files, its just \n.
Unfortunately, you can't do that in Notepad++ when using regex search. Notepad++ is based on the Scintilla editor component, which doesn't handle newlines in regex.
You can use extended search for newline searching, but I don't think that will help you search for 3 lines.
More info here.
Update: Robb and StartClass0830 were right about extended search. It does work, but not when using regular expressions search.
^a\x0D\x0Ab\x0D\x0Ac
This will work \x0D is newline and \x0A is carriage return. Assumption is that each line in your file ends with ascii 10 and 13.
I found a workaround for this.
Simply, in Extended mode replace all \r\n to a string that didn't exist in the rest of the document eg. ,,,newline,,, (watch out for special regexp chars like $, &, and *).
Then switch to Regexp mode, do some replacements (now newline is ,,,newline,,,).
Next, switch to Extended mode again and replace all ,,,newline,,, to \r\n.
For Notepad 6 and beyond, do this as a regular expression:
Select Search Mode > Regular expression (w/o . matches newline)
And in the Find what Textbox : a[\r\n]b[\r\n]+c[\r\n]
or if you are looking at the (Windows or Unix) file to see its line breaks as \r\n or \n then you may find it easier to use Extended Mode:
Select Search Mode > Extended (\n, \r, \t, \0, \x...)
And in the Find what Textbox for Windows: a\r\nb\r\nc\r\n
Or in the Find what Textbox for Unix: a\nb\nc\n
Wasn't clear if the OP intent is to select the trailing line return (after the 'c') as well, as would be necessary to remove the lines.
To not select the trailing line return, as appropriate for replacing with a non-empty string, simply remove the final line return from the matching statement.
Note that if there should be a match on the last line of the string, without a matching trailing line return, the match fails.
a\r\nb\r\nc works for me, but not ^a\x0D\x0Ab\x0D\x0Ac
Hmm, too bad that newline is not working with regular expressions. Now I have to go back to Textpad again. :(
Select Search Mode Which is
Extended (\n, \r, \t, \0, \x...)
\n is new line and such
This is Manuel
Find: "(^a.$)\r\n(b.)\r\n^(c.*)$" - pickup 3 whole lines, only storing data
Replace with: "\1\2\3" - Put down (replay) data
Works fine in Regex with Notepad++ v7.9.5
Place holders: ^ Start and $ End of line can be inside or out of ()store as shown, though clearly not necessary in given example. Note "[^x]" is different - here "^" is "NOT".
Advantage of storing and replay allows much more complicated pattern match without having to type in again what you want to end up with, and even change of replay: "\2\3\1" for "bca"
I have run accross this little issue when the document is windows CR/LF
If you click the box for . to match newlines you need .. to match CR/LF so if you have
<blah><blah>",
"<more><blah>
you need to use ",.." to match some string comma cr/lf another string
In Notepad++ you can also try highlighting the desired part of the text and then pressing CTRL+J.
That would justify the text and thus removing all line endings.

How to search and replace 2 lines (together) in 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.