IntelliJ Script to Replace Text - regex

I have a log statement that looks like this:
Log.debug("text={}", "something");
That I want to replace with:
Log.debug("something");
Instead of doing a ctrl+F and going one by one to replace this is there a way to replace everything at once with any combination of intelliJ macros + scripting + regex?

There are two ways of doing this:
1.) You could use the ctrl + f way, but search for the entire statement
Log.debug("text={}", "something");
and then go to the replace textbox and type the desired statement
Log.debug("something");
and then just hit the Replace All option.
2.) The other way is with a regex, but you will invest more time in creating a regex that match what you need and then replace it with the statement you want. I recommend you using the first way since it would minimize the time invested.
EDIT.
There are 2 more ways:
The 3rd way, you could use the Add Selection for Next Occurrence short cut (hit ALT+CTRL+S and search for that option and see its shortcut or assign one if there isn't one) and then just highlight the statement you want and hit that shortcut until you find them all and then type the desired statement.
The 4th way, highlight the statement and hit the Rename... shortcut (find the shortcut the same way explained on the 3rd way) and it will refactor all the matches.
Careful with the last one because intellij sometimes match all those occurrencies in all the project/projects you have open at that time in the same project structure; Of course, you could check and uncheck the ones you want to change and those who you don't want to.
Let me know if any of these possible solutions worked for you =).

Related

If duplicate within brackets, delete one of the lines

Hi i have a long list of items (~6k), that comes in this format:
'Entry': ['Entry'],
What i want to do, is if within the first bracket, the words match, i.e.:
'ACT': ['KOSOV'],
'ACT': ['STIG'],
I want it to leave only one of the entries, it doesn't matter which entry the first the second or whatever, i just need it to leave one of them.
If possible I would like to accomplish that by sublime, or notepad++ using regexp and if there is no way then do whatever you think is best to solve this.
UPD: The AWK command did the job indeed, thank you
You can't solve this using just regular expressions. You either need to remember all entries you've seen so far while scanning the text (would require writing a small utility program, probably), or you could sort the entries and then remove any repeated entries.
If you have a sorted file, then you can solve it using a regular expression, such as this one:
^(([^:]+):.+\n)(?:\2.+\n)+
Replace with \1. See it in action here

Notepad++ Regex - Finding and replacing multiple different criteria simultaneously

I've just started to get to grips with regex in notepad++ and I've tasked myself with formatting a chunk of JSON data into something human readable, as well as something that can be read into an algorithm a colleague of mine wrote. I've found a few regex expressions that do this perfectly, but in order to get to my desired result, I have to do it in four separate Find/Replace steps. Is there some sort of way I can create one single find/replace expression that handles all of the above tasks for me?
Currently I have Notepad++ doing the following:
Deleting all quotation marks by finding " and replacing it with
nothing
Deleting all commas by finding , and replacing it with nothing
Changing all underscored numbers that are followed by a colon with
the number 0 (the reason behind this is particular to the project)
by finding _[0-9]*: and replacing with _0 and finally, putting all
of a particular expression onto it's own line by finding the start
of the particular string I'm after and adding \n.
I know that's convoluted, but fortunately it does the job. Is there any way of consolidating all that into a single command, or does that all have to be done step by step?
Thanks guys :)
Notepad ++ allows you to consolidate individual search and replaces as a macro which you can also save.
Hit the record button in the toolbar (or Macro>Start Recording)
perform these regex replacements in the required order.
hit stop button in toolbar (or Macro>Stop Recording)
Hit the play button to perform all the required replacement operations again.
Save the macro by going into the Macro option in the window menu and 'save current recorded macro'
As for the first to replacements you could use the following expression: (?:"|,)

How do I join two regular expressions into one in Notepad++?

I've been searching a lot in the web and in here but I can't find a solution to this.
I have to make two replacements in all registry paths saved in a text file as follows:
replace all asterisc with: [#42]
replace all single backslashes with two.
I already have two expressions that do this right:
1st case:
Find: (\*) - Replace: \[#42\]
2nd case:
Find: ([^\\])(\\)([^\\]) - Replace: $1$2\\$3
Now, all I want is to join them together into just one expression so that I can do run this in one time only.
I'm using Notepad++ 6.5.1 in Windows 7 (64 bits).
Example line in which I want this to work (I include backslashes but i don't know if they will appear right in the html):
HKLM\SOFTWARE\Classes\*\shellex\ContextMenuHandlers\
I already tried separating it with a pipe, like I do in Jscript (WSH), but it doesn't work here. I also tried a lot of other things but none worked.
Any help?
Thanks!
Edit: I have put all the backslashes right, but the page html seem to be "eating" some of them!
Edit2: Someone reedited my text to include an accent that doesn't remove the backslashes, so the expressions went wrong again. But I got it and fixed it. ;-)
Sorry, but this was my first post here. :)
As everyone else already mentioned this is not possible.
But, you can achieve what you want in Notepad++ by using a Macro.
Go to "Macro" > "Start Recording" menu, apply those two search and replace regular expressions, press "Stop Recording", then "Save Current Recorded Macro", there give it a name, assign a shortcut, and you are done. You now can reuse the same replacements whenever you want with one shortcut.
Since your replacement strings are totally different and use data that come not from any capture (i.e. [#42]), you can't.
Keep in mind that replacement strings are only masks, and can not contain any conditional content.

Different replace for the same word notepad++

I have these words: TOP and LOCK. I have 49 times this word but I would like to replace it with a different word a lot of times. For example, the first time with luc and the second times with pile.
I would like to make it automatically. I have the list of the replace words in a text file. I use Notepad++.
I have searched on many sites, but cannot find a good solution.
I don't have notepad ++ but can you highlight and right click there's an option to replace? or highlighting it then clicking format at the top and clicking replace and finally using Ctrl + F and replacing it that way? sorry if i could not help :)
try this:
tap ctrl + F
you can look at this pisture to get the picture:
http://im34.gulfup.com/Vkws5.jpg
Hope it help you
Highlight TOP
Press Search on top menu bar
Go to Replace
Under Find What, in Replace with field type what you want to replace it with
Then hit Replace All
You will then see the changes
I'm not very familiar with Notepad++ but I can do this with Sublime Text. I guess it doesn't hurt for you to try it out.
First, select all the words you want to replace with multiple cursor mode. Basically you can find TOP|LOCK with regular expressions, and hit Alt+Enter to convert the found words to multiple cursors.
Second, copy the words you want to replace and use the Text Pastry plugin to paste them in, one word at each cursor.
See here for a demo: http://imgur.com/a/KrBB8#0

Regex to change to sentence case

I'm using Notepad++ to do some text replacement in a 5453-row language file. The format of the file's rows is:
variable.name = Variable Value Over Here, that''s for sure, Really
Double apostrophe is intentional.
I need to convert the value to sentence case, except for the words "Here" and "Really" which are proper and should remain capitalized. As you can see, the case within the value is typically mixed to begin with.
I've worked on this for a little while. All I've got so far is:
(. )([A-Z])(.+)
which seems to at least select the proper strings. The replacement piece is where I'm struggling.
Find: (. )([A-Z])(.+)
Replace: \1\U\2\L\3
In Notepad++ 6.0 or better (which comes with built-in PCRE support).
Regex replacement cannot execute function (like capitalization) on matches. You'd have to script that, e.g. in PHP or JavaScript.
Update: See Jonas' answer.
I built myself a Web page called Text Utilities to do that sort of things:
paste your text
go in "Find, regexp & replace" (or press Ctrl+Shift+F)
enter your regex (mine would be ^(.*?\=\s*\w)(.*)$)
check the "^$ match line limits" option
choose "Apply JS function to matches"
add arguments (first is the match, then sub patterns), here s, start, rest
change the return statement to return start + rest.toLowerCase();
The final function in the text area looks like this:
return function (s, start, rest) {
return start + rest.toLowerCase();
};
Maybe add some code to capitalize some words like "Really" and "Here".
In Notepad++ you can use a plugin called PythonScript to do the job. If you install the plugin, create a new script like so:
Then you can use the following script, replacing the regex and function variables as you see fit:
import re
#change these
regex = r"[a-z]+sym"
function = str.upper
def perLine(line, num, total):
for match in re.finditer(regex, line):
if match:
s, e = match.start(), match.end()
line = line[:s] + function(line[s:e]) + line[e:]
editor.replaceWholeLine(num, line)
editor.forEachLine(perLine)
This particular example works by finding all the matches in a particular line, then applying the function each each match. If you need multiline support, the Python Script "Conext-Help" explains all the functions offered including pymlsearch/pymlreplace functions defined under the 'editor' object.
When you're ready to run your script, go to the file you want it to run on first, then go to "Scripts >" in the Python Script menu and run yours.
Note: while you will probably be able to use notepad++'s undo functionality if you mess up, it might be a good idea to put the text in another file first to verify it works.
P.S. You can 'find' and 'mark' every occurrence of a regular expression using notepad++'s built-in find dialog, and if you could select them all you could use TextFX's "Characters->UPPER CASE" functionality for this particular problem, but I'm not sure how to go from marked or found text to selected text. But, I thought I would post this in case anyone does...
Edit: In Notepad++ 6.0 or higher, you can use "PCRE (Perl Compatible Regular Expression) Search/Replace" (source: http://sourceforge.net/apps/mediawiki/notepad-plus/?title=Regular_Expressions) So this could have been solved using a regex like (. )([A-z])(.+) with a replacement argument like \1\U\2\3.
The questioner had a very specific case in mind.
As a general "change to sentence case" in notepad++
the first regexp suggestion did not work properly for me.
while not perfect, here is a tweaked version which
was a big improvement on the original for my purposes :
find: ([\.\r\n][ ]*)([A-Za-z\r])([^\.^\r^\n]+)
replace: \1\U\2\L\3
You still have a problem with lower case nouns, names, dates, countries etc. but a good spellchecker can help with that.