Multiline search replace with regexp in Eclipse - regex

Eclipse regexp search works pretty well, so for example in search box I have this:
(?s)(myMethod.*?;)\}\);
Now I want to copy multiline text in the IDE and in replace box, for example I want to paste \1PASTE_MULTILINE_TEXT_HERE. However Eclipse does not allow me to directly copy-paste multiline text without manually inserting newline characters.
In Vim (Gvim, Macvim) it works perfectly well, keeping all the spaces; how can I do the same thing in Eclipse?

For searching multiple lines in Eclipse, you must use the 's' parameter in search expression:
(?s)someExpressionToMatchInAnyLine
For replacing with multiple lines exp you must use \R i.e:
line1\Rline2\Rline3
This will replace the matched exp with:
line1
line2
line3

Generally, the approach I've taken to doing this sort of thing is to type out what I want to use as a replacement, select that, open up the Find/Replace dialog, and copy the contents of the Find text box. I proceed from there and paste what I copied into the Replace text box. There is still a little work to be done (removing backslashes from in front of regex special characters that don't apply in the Replace box), but it gives me a hand up.

Related

Regex and replace selected results only

I want to know whether there is any tool that does a regex search across huge text (xml or tagged or html) and replace only the cases which are selected across shown (should have 'select/deselect/select all' options while applying the replace regex).
Like the below example:
My content is:
"Visited xtreme.com, stupid.net, childish.com, happy.net and innocence.edu. There are some cross.network isssues that are to be fixed."
Now in this content, i want to replace all ".net" occurrences with ".com" and so a simple tool like notepad++ would replace it easily. But i want the tool to show the search results and give the option to replace only first two occurrences of ".net" and not the instance in "cross.network"
This is only example purposes only and don't suggest an alternate regex. I don't need it.
NP++ or sublime are all fine, as long as they can read all the text to the memory. They both support regexes for finding and replacing text.
If the text files are too big, i.e. NP++ crashes, then you can use sed. It's a command line tool that can replace text like this:
sed -i filename.txt 's/pattern/replacement/g'
On windows boxes you need mingw or cygwin to run it.
Use a text editor like sublime and apply a word boundary to the regular expression:
\.net\b
This will find .net in stupid.net but not in cross.network.
See a demo on regex101.com.

Notepad++ Regex inverse match

I'm new to Regex and trying to figure out how to remove all text from file open in Notepad++ that does not match #LCxxxx or #LAxxxx. Example below (text wanting to keep in bold):
1.In rare cases, reinstalling this MSP file can cause the Citrix Display Driver.....
[From ICAWS760WX86][#0528688]
30.This release includes an enhancement...
[From ICAWS760WX86022][#LA3014]
New Fixes in This Release
1.Windows Server 2008 R2 and Windows Server 2012 R2,...
[From ICAWS760WX86026][#LC2179]
Fixes from Replaced Hotfixes
1.If the Windows Remote Desktop Session Host....
[From ICAWS760WX86004][#LC1180]
I think this is what you're looking for:
(?:[\S\s]*?)(\#L[AC]\d{4})(?:.*)
Replace with:
$1\n
You could do a regular expression search and replace, searching for
(#L[AC]....)
where "dot matches newline" is NOT selected. Replace with
\r\n\1\r\n
That will put all the wanted pieces of text on a line on their own.
Next use the "Mark" tab in the find window. Select "Bookmark line", use the same search string as above (the capture brackets are not needed this time, but they are harmless and so can be left), and them click "Mark all". Now all the wanted lines are bookmarked. Use menu => Search => Bookmark => Remove unmarked lines.
There may be a way of doing it all in one go, but that would be a complex regular expression. The method above uses two simple steps.
remove all text from file open in Notepad++ that does not match #LCxxxx or #LAxxxx
^.*(\[#L[CA]\d+\])$|^.*$
DESCRIPTION
DEMO
https://regex101.com/r/hO1aL8/2
Notepad++
Do a search and replace like describe in the screenshot below:
Alternatively, if you want to get rid off the empty lines during the replace operation, use the regular expression below:
^[\S\s]+?(\[#L[CA]\d+\])$
\s : Whitespaces (\t,\r,\n ...)
\S : Any character except whitespaces.
Tested on Notepad 6.6.9

Need help using RegEx to Replace lines

I need to do a Find & Replace (NOT in Code, in the VB IDE or a Text Editor).
I need to take:
IsNumeric(CInt(whatevertextishere))
and change it to
IsNumeric(whatevertextwasthere)
I've tried:
IsNumeric\(CInt\(.*\)\)
for find, and IsNumeric($1) for the replace, the find works, but the replace does not. It simply replaces it with the literal text IsNumeric($1), instead of putting the matched text, which should be the "whatevertextishere".
What am I doing wrong?
You haven't defined a capture group in the regex (with unescaped parentheses):
IsNumeric\(CInt\((.*)\)\)

Notepad++ replace two lines with other (10) lines in open documents

I want to replace two lines with other lines in Notepad++.
The main problem is that I am not able to copy all the lines which should be replaced. Only the first line is inserted in the "Replace with:" input field if I paste all lines in the field. It seems that the line break is not correctly copied.
Selecting the lines (with the line break) which should be inserted in the "Find what:" field is quite easy because I can select them in the document and simply hit "CTRL + H".
What to do? Please no solutions how it could work with command line tools.
Regards
Albeit a bit late for an answer, I think it's OK.
You can not search for a multi line string in Notepad++ using the normal search mode.You should use the extended search mode instead.
You just have to escape the new lines.What's best, you can use Notepad++ to prepare the escaped text to be searched and replaced.
I assume you are using windows text files meaning the new line is represented with \r\n
To achieve what you want:
1.
Create a new document and paste your multiline text to be replaced
Do a replace on it using the extended search mode. Find what: \r\n Replace With:\\r\\n
The result will be your "Find what" string.
2.
Create a new document and paste your multiline replacement text
Do a replace on it using the extended search mode. Find what: \r\n Replace With:\\r\\n
The result will be your "Replace with" string.
3.
Now that you have your escaped data, do a Replace on all the open documents using the extended search mode AND the results from the previous steps.
Hope this helps.
None of these suggestions are acceptable! TextFX's Ctrl+R replace plugin falls way short.
What EVERYONE wants, everyone that wants to perform a replacement of multiline blocks of text with another multiline block of text, is this...
2 large text boxes:
Find This:
This is line one
This is LIKE two
This is line TREE
Replace with This:
This is line 1
This is line 2
This is line 3
A checkbox for "All Open Documents"
And/Or...
Option for "Find-Replace in all Files of Type"
Then a GO button............
How hard could that be to create in Notepad++? It was done way back in 1998, a freeware utility called Search-Replace 98.
UPDATE:
The plugin suggested by numediaweb DOES EXACTLY what I needed! Hats off to numediaweb for the tip and standing applause for paul at phdesign!
ToolBucket multi-line search plugin for Notepad++
http://www.phdesign.com.au/programming/toolbucket-multi-line-search-plugin-for-notepad/
ToolBucket contains the following features:
Multi-line search and replace dialog.
Change indentation dialog.
Generate GUID
Generate Lorem Ipsum
Compute MD5 Hash
Compute SHA1 Hash
Base 64 encode
Base 64 decode
Download
The latest version is available here:
https://github.com/phdesign/NppToolBucket/downloads
For regular expressions you can use Ctrl-R, aka TextFX -> TextFX Quick -> Find/Replace.
If not check this plugin, it does what you want!
Based on the response of Nikanos Polykarpou below is my...
Notepad++ - Replace by a multiple lines string
Select the string to replace (can have multiple lines).
Follow...
Ctrl+h -> Replace (tab) -> Enable "Extended (\n, \r, \t, \0, \x...)"
... in "Replace with:" enter a string to do the replace with "\r\n" (if Windows) instead of real line breaks as this example...
"model" "models/aztec100500/flo_grass.mdl"\r\n"framerate" "10"\r\n"angles" "0 30 0"\r\n"classname" "cycler_sprite"
... do the replace!

Multiline Regular Expression search and replace!

I've hit a wall. Does anybody know a good text editor that has search and replace like Notepad++ but can also do multi-line regex search and replace? Basically, I am trying to find something that can match a regex like:
search oldlog\(.*\n\s+([\r\n.]*)\);replace newlog\(\1\)
Any ideas?
Notepad++ can now handle multi line regular expressions (just update to the latest version - feature was introduced around March '12).
I needed to remove all onmouseout and onmouseover statements from an HTML document and I needed to create a non-greedy multi line match.
onmouseover=.?\s*".*?"
Make sure you check the: [ ] . matches newline checkbox if you want to use the multi line match capability.
EditPad Pro has better regex capabilities than any other editor I've ever used.
Also, I suspect you have an error in your regex — [\r\n.] will match only carriage returns, newlines, and full stops. If you're trying to match any character (i.e. "dot operator plus CR and LF), try [\s\S] instead.
My personal recommendation is IDM Computing's UltraEdit (www.ultraedit.com) - it can do regular expressions (both search and replace) with Perl, Unix and UltraEdit syntax. Multi-line matching is one of the capabilities in Perl regex mode in it.
It also has other nice search capabilities (e.g search in specific character column range, search in multiple files, search history, search favorites, etc...)
(source: ultraedit.com)
The Zeus editor can do multi-line search and replace.
I use Eclipse, which is free and that you may already have if you are a developer. '\R' acts as platform independent line delimiter. Here is an example of multi-line search:
search:
\bibitem.(\R.)?\R?{([^{])}$\R^([^\].[^}]$\R.$\R.)
and replace:
\defcitealias{$2}{$3}
I'm pretty sure Notepad++ can do that now via the TextFX plugin (which is included by default). Hit Control-R in Notepad++ and have a play.
TextPad has good Regex search and replace capabilities; I've used it for a while and am pretty happy with it.
From the Features:
Powerful search/replace engine using
UNIX-style regular expressions, with
the power of editor macros. Sets of
files in a directory tree can be
searched, and text can be replaced in
all open documents at once.
For more options than you could possibly need, check out "Notepad++ Alternatives" at AlternativeTo.net.
you can use Python Script plugin for Multiline Regular Expression search and replace!
- http://npppythonscript.sourceforge.net/docs/latest/scintilla.html?highlight=pymlreplace#Editor.pymlreplace
# This example replaces any <br/> that is followed by another on the next line (with optional spaces in between), with a single one
editor.pymlreplace(r"<br/>\s*\r\n\s*<br/>", "<br/>\r\n")
I use Notepad++ all the time but it's Regex has alway been a bit lacking.
Sublime Text is what you want.
EditPlus does a good job at search/replace using regex (including multiline)
You could use Visual Studio. Download Express for free if you don't have a copy.
VS's regex is non-standard, so you'd have to use \n:b+[\r\n] instead.
The latest version of UltraEdit has multiline find and replace w/ regex support.
Or if you're OK with using a more specialized regular expression tool for this, there's Regex Hero. It has the side benefit of being able to do everything on the fly. In other words, you don't have to click a button to test your regular expression because it's automatically tested after every keypress.
Personally, I'd use UltraEdit if I'm looking to replace text in multiple files. That way I can just select the files to replace as a batch and click Replace. But if I'm working with a single text file and I'm in need of writing a more complex regular expression then I'd paste it into Regex Hero and work with it there. That's because Regex Hero can save time when you see everything happen in real-time.
ED for windows has two versions of regex, three sorts of cut and paste (selection, lines or blocks, AND you can shift from one to the next (unlike ultra edit, which is clunky at best) with just a mouse click while you are highlighting -- no need to pull down a menu. The sheer speed of getting the job done is incredible, like reading on a Kindle, you don't have to think about it.
You can use a recent version of Notepad++ (Mine is 6.2.2).
No need to use the option ". match newline" as suggested in another answer. Instead, use the adequate regular expression with ^ for "begin of line" and $ for "end of line". Then use \r\n after the $ for a "new line" in a dos file (or just \n in a unix file as the carriage return is mainly used for dos/windows text file):
Ex.: to remove all lines starting with tags OBJE after a line starting with a tag UID (from a gedcom file - used in genealogy), I did use the following search regex:
^UID (.*)$\r\n^(OBJE (.*)$\r\n)+
And the following replace value:
UID \1\r\n
This is matching lines like this:
UID 4FBB852FB485B2A64DE276675D57A1BA
OBJE #M4#
OBJE #M3#
OBJE #M2#
OBJE #M1#
and the output of the replacement is
UID 4FBB852FB485B2A64DE276675D57A1BA
550 instances have been replaced in less than 1 sec. Notepad++ is really efficient!
Otherwise, to validate a Regular expression I like to use the .Net RegEx Tester (http://regexhero.net/tester/). It's really great to write and test on the fly a Reg Ex...
PS.: Also, you can use [\s\S] in your regex to match any character including new lines. So, if you look for any block of "multi-line" text starting with "xxx" and ending with "abc", the following Regex will be fine:^xxx[\s\S]*?abc$ where "*?" is to match as less as possible between xxx and abc !!!