Notepad++ Regex inverse match - regex

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

Related

Match all spaces after a particular string

Doing a find and replace in VsCode on a large amount of files. I'm looking to replace all spaces after a set of quotes, but only on a specific line.
I can very easily find all spaces using \s+, but I don't understand how to capture only the spaces after a specific string(one specific line). I've tried positive look behinds, but I can only get it to match the first space, but I need to match all spaces on that line.
Example code:
variable = "01 - Testing this thing"
I need to find and replace all the spaces between the quotation marks with underscores, but I can't get any regex to match all the spaces between the quotes. I might want to replace the dash(-) as well, but the spaces are more important and I'm struggling to figure it out.
Here is a pretty good workflow.
Open a Search Editor (from the Command Palette or set a keybinding to it).
Use this regex (?<=variable = ")[^"]*.
That will find all matches in all files in your workspace or whatever folders you designate in the file to include filter. I suggest setting the context lines option to 0.
Ctrl+Shift+L to select all your matches. The matches are the 01 - Testing this thing part.
Now do a regular find in that search editor tab - with the Find in Selection option enabled.
Simply doing a find of and replaceAll with _ will make all those changes (in the Search Editor only).
To apply those changes to all the files with your initial search results, use the extension search-editor-apply-changes Apply Search Editor Changes... command.
Then you can check to see if the changes were as you expected and save all. It will open all affected files so you can inspect them.
Seems like a few steps but notice the first regex can be very simple. And then you are doing a simple find/replace in just those selections. Demo:
You search for a string that matches, it has A space between the quotes. Replace with what is before and after the space but the space is now an underscore. You have to apply this as often as the max number od spaces in a string. It can't be done in 1 regex search-replace.
In the Search Bar
Find Regex:
(variable = "[^" ]*) ([^"]*")
Replace:
$1_$2
Then apply Replace All (button) and Refresh (button) until no more searches found.

Remove everything before and after variable=int

I'm terrible at regex and need to remove everything from a large portion of text except for a certain variable declaration that occurs numerous times, id like to remove everything except for instances of mc_gross=anyint.
Generally we'd need to use "negative lookarounds" to find everything but a specified string. But these are fairly inefficient (although that's probably of little concern to you in this instance), and lookaround is not supported by all regex engines (not sure about notepad++, and even then probably depends on the version you're using).
If you're interested in learning about that approach, refer to How to negate specific word in regex?
But regardless, since you are using notepad++, I'd recommend selecting your target, then inverting the selection.
This will select each instance, allowing for optional white space either side of the '=' sign.
mc_gross\s*=\s*\d+
The following answer over on super user explains how to use bookmarks in notepad++ to achieve the "inverse selection":
https://superuser.com/questions/290247/how-to-delete-all-line-except-lines-containing-a-word-i-need
Substitute the regex they're using over there, with the one above.
You could do a regular expression replace of ^.*\b(mc_gross\s*=\s*\d+)\b.*$ with \1. That will remove everything other than the wanted text on each line. Note that on lines where the wanted text occurs two or more times, only one occurrence will be retained. In the search the ^.*\b matches from start-of-line to a word boundary before the wanted text; the \b.*$ matches everything from a word boundary after the wanted text until end of line; the round brackets capture the wanted text for the replacement text. If text such as abcmc_gross=13def should be matched and retained as mc_gross=13 then delete the \bs from the search.
To remove unwanted lines do a regular expression search for ^mc_gross\s*=\s*\d+$ from the Mark tab, tick Bookmark line and click Mark all. Then use Menu => Search => Bookmark => Remove unmarked lines.
Find what: [\s\S]*?(mc_gross=\d+|\Z)
Replace with: \1
Position the cursor at the start of the text then Replace All.
Add word boundaries \b around mc_gross=\d+ if you think it's necessary.

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

Find CRLF in Notepad++

How can I find/replace all CR/LF characters in Notepad++?
I am looking for something equivalent to the ^p special character in Microsoft Word.
[\r\n]+ should work too
Update March, 26th 2012, release date of Notepad++ 6.0:
OMG, it actually does work now!!!
Original answer 2008 (Notepad++ 4.x) - 2009-2010-2011 (Notepad++ 5.x)
Actually no, it does not seem to work with regexp...
But if you have Notepad++ 5.x, you can use the 'extended' search mode and look for \r\n. That does find all your CRLF.
(I realize this is the same answer than the others, but again, 'extended mode' is only available with Notepad++ 4.9, 5.x and more)
Since April 2009, you have a wiki article on the Notepad++ site on this topic:
"How To Replace Line Ends, thus changing the line layout".
(mentioned by georgiecasey in his/her answer below)
Some relevant extracts includes the following search processes:
Simple search (Ctrl+F), Search Mode = Normal
You can select an EOL in the editing window.
Just move the cursor to the end of the line, and type Shift+Right Arrow.
or, to select EOL with the mouse, start just at the line end and drag to the start of the next line; dragging to the right of the EOL won't work.
You can manually copy the EOL and paste it into the field for Unix files (LF-only).
Simple search (Ctrl+F), Search Mode = Extended
The "Extended" option shows \n and \r as characters that could be matched.
As with the Normal search mode, Notepad++ is looking for the exact character.
Searching for \r in a UNIX-format file will not find anything, but searching for \n will. Similarly, a Macintosh-format file will contain \r but not \n.
Simple search (Ctrl+F), Search Mode = Regular expression
Regular expressions use the characters ^ and $ to anchor the match string to the beginning or end of the line. For instance, searching for return;$ will find occurrences of "return;" that occur with no subsequent text on that same line. The anchor characters work identically in all file formats.
The '.' dot metacharacter does not match line endings.
[Tested in Notepad++ 5.8.5]: a regular expression search with an explicit \r or \n does not work (contrary to the Scintilla documentation).
Neither does a search on an explicit (pasted) LF, or on the (invisible) EOL characters placed in the field when an EOL is selected.
Advanced search (Ctrl+R) without regexp
Ctrl+M will insert something that matches newlines. They will be replaced by the replace string.
I recommend this method as the most reliable, unless you really need to use regex.
As an example, to remove every second newline in a double spaced file, enter Ctrl+M twice in the search string box, and once in the replace string box.
Advanced search (Ctrl+R) with Regexp.
Neither Ctrl+M, $ nor \r\n are matched.
The same wiki also mentions the Hex editor alternative:
Type the new string at the beginning of the document.
Then select to view the document in Hex mode.
Select one of the new lines and hit Ctrl+H.
While you have the Replace dialog box up, select on the background the new replacement string and Ctrl+C copy it to paste it in the Replace with text input.
Then Replace or Replace All as you wish.
Note: the character selected for new line usually appears as 0a.
It may have a different value if the file is in Windows Format. In that case you can always go to Edit -> EOL Conversion -> Convert to Unix Format, and after the replacement switch it back and Edit -> EOL Conversion -> Convert to Windows Format.
It appears that this is a FAQ, and the resolution offered is:
Simple search (Ctrl+H) without regexp
You can turn on View/Show End of Line
or view/Show All, and select the now
visible newline characters. Then when
you start the command some characters
matching the newline character will be
pasted into the search field. Matches
will be replaced by the replace
string, unlike in regex mode.
Note 1: If you select them with the
mouse, start just before them and drag
to the start of the next line.
Dragging to the end of the line won't
work.
Note 2: You can't copy and paste
them into the field yourself.
Advanced search (Ctrl+R) without regexp
Ctrl+M will insert something that matches newlines. They will be replaced by the replace string.
On the Replace dialog, you want to set the search mode to "Extended". Normal or Regular Expression modes wont work.
Then just find "\r\n" (or just \n for unix files or just \r for mac format files), and set the replace to whatever you want.
I've not had much luck with \r\n regular expressions from the find/replace window.
However, this works in Notepad++ v4.1.2:
Use the "View | Show end of line" menu to enable display of end of line characters.
(Carriage return line feeds should show up as a single shaded CRLF 'character'.)
Select one of the CRLF 'characters' (put the cursor just in front of one, hold down the SHIFT key, and then pressing the RIGHT CURSOR key once).
Copy the CRLF character to the clipboard.
Make sure that you don't have the find or find/replace dialog open.
Open the find/replace dialog.
The 'Find what' field shows the contents of the clipboard: in this case the CRLF character - which shows up as 2 'box characters' (presumably it's an unprintable character?)
Ensure that the 'Regular expression' option is OFF.
Now you should be able to count, find, or replace as desired.
Image with CRLF
Image without CRLF
The way I found it to work is by using the Replace function, and using "\n", with the "Extended" mode. I'm using version 5.8.5.
In 2013, v6.13 or later, use:
Menu Edit → EOL Conversion → Windows Format.
To find any kind of a line break sequence use the following regex construct:
\R
To find and select consecutive line break sequences, add + after \R: \R+.
Make sure you turn on Regular expression mode:
It matches:
U+000DU+000A -CRLF` sequence
U+000A - LINE FEED, LF
U+000B - LINE TABULATION, VT
U+000C - FORM FEED, FF
U+000D - CARRIAGE RETURN, CR
U+0085 - NEXT LINE, NEL
U+2028 - LINE SEPARATOR
U+2029 - PARAGRAPH SEPARATOR
Assuming it has a "regular expressions" search, look for \r\n. I prefer \r?\n, because some files don't use carriage returns.
EDIT: Thanks for the feedback, whoever voted this down. I have learned that... well, nothing, because you provided no feedback. Why is this wrong?
Use the advanced search option (Ctrl + R) and use the keyboard shortcut for CRLF (Ctrl + M) to insert a carriage return.
If you need to do a complex regexp replacement including \r\n, you can workaround the limitation by a three-step approach:
Replace all \r\n by a tag, let's say #GO# → Check 'Extended', replace \r\n by #GO#
Perform your regexp, example removing multiline ICON="*" from an html bookmarks → Check regexp, replace ICON=.[^"]+.> by >
Put back \r\n → Check 'Extended', replace #GO# by \r\n
Go to View--> Show symbol-->Show all character
// Its worked for me
Make this setting. Menu-> View-> Show Symbol-> uncheck Show End of the Line
I opened the file in Notepad++ and did a replacement in a few steps:
Replace all "\r\n" with " \r\n"
Replace all "; \r\n" with "\r\n"
Replace all " \r\n" with " "
This puts all the breaks where they should be and removes those that are breaking up the file.
It worked for me.
I was totally unable to do this in NP v6.9.
I found it easy enough on Msoft Word (2K).
Open the doc, go to edit->replace.
Then in the bottom of the search box, click "more" then find the "Special" button and they have several things for you. For Dos style, I used the "paragraph" one. This is a cr lf pair in windows land.
Just do a \r with a find and replace with a blank in the replace field so everything goes up to one line. Then do a find and replace (in my case by semi colon) and replace with ;\n
:)
-T&C
To change a document of separate lines into a single line, with each line forming one entry in a comma separated list:
ctrl+f to open the search/replacer.
Click the "Replace" tab.
Fill the "Find what" entry with "\r\n".
Fill the "Replace with" entry with "," or ", " (depending on preference).
Un-check the "Match whole word" checkbox (the important bit that eludes logic).
Check the "Extended" radio button.
Click the "Replace all" button.
These steps turn e.g.
foo bar
bar baz
baz foo
into:
foo bar,bar baz,baz foo
or: (depending on preference)
foo bar, bar baz, baz foo
Maybe you can use TextFX plugins
In TextFX, go to textfx edit → delete blank lines