Visual Studio append text to end of lines using find/replace with end line regular expression ($) - regex

I am trying to append some text (e.g. "Fish") to the end of every line in a file using Visual Studio or SQL Server Management Studio using the following settings in the find/replace dialog:
Find what: $
Replace with: Fish
Use Regular expressions: Checked
This mostly does the job, but for a handful of lines it not only appends "Fish" to the end of the line it also puts it at the beginning of the line. I can't discern any pattern to this behaviour it seems to be almost random, with the larger the file the more lines tending to go wrong.
A similar find/replace with ^ (to put text at the beginning of the line) works with no problem.
Anybody know why this is happening? And also, are there any better suggestions for achieving what I want to?

This works in Visual Studio 2012 and 2015:
Find: \r
Replace: Fish\r
Make sure you tick 'Use Regular Expressions' checkbox:

I'm not sure why you're seeing that, but you might try something like:
Find: ^.*$
Replace: \0Fish

Related

How do I replace text with a linebreak using regular expressions

I have written a program that searches for 'opens' without matching 'closes' in a target file. It produces an output like this:
open
close
open
open
close
In this example here the second 'open' is not immediately followed by a matching 'close', so I consider that an error. My text-editor (editpad) has a regular-expression search/replace feature, which I would like to use to get rid of all correct pairs, so I can notice easily the incorrect situations. I tried replacing the following
open\r\nclose
I thought that this would match the two words and the line-break between them (I'm using Windows). This did not work.
Does anyone have an idea why it didn't?
If you select the Regex mode, your regex will work, but I also suggest making \r optional by appending ? after it to support LF endings, too:
open\r?\nclose
See the screenshot with settings and proof it works in EditPad:

Visual Studio hangs searching for regex ^.*$

(To clarify, I'm talking about Ctrl-Shift-F search. Current Document.)
I want to search for lines that don't contain a certain character, like '(', so I figure I need to include ^ and $ indicators to get the entire line. But this just crashes the GUI. Is there a way forward?
Lines without ( should be handled by this:
^[^\(]*$
But this hangs, as does the simpler "^.*$".
Maybe there's another way to find these lines?
EDIT: the proposed "duplicate" question is about C# RegEx class, completely utterly different from Visual Studio 2010 interactive regular expressions.
If you want to match lines, use ^[^\r\n(]*$, where \r and \n are excluded from [^(] – Wiktor Stribiżew Jun 27 at 19:36
This comment is indeed the answer. Apparently old Visual Studio searches could span multiple lines, and ^.*$ thus means start at the beginning of the line, go forward any number of characters up to the end of the document, then stop at the end of a line. These characters can include any number of newlines.
VS apparently didn't efficiently implement this query and hangs interminably.
W.S.'s proposal explicitly excludes newlines from the search parameters, thereby forcing only a single line.
Visual Studio 2012 changes to more conventional regular expression search which limits results to within single lines, so ^.*$ will just fetch all the lines of a document one by one.

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

Multi-line regular expressions in Visual Studio

Is there any way to get Visual Studio to perform a regex replace across multiple lines (let the match cross line boundaries)? I know there are many editors I can use for this, but it seems strange that this feature has been left out of Visual Studio. Am I missing something?
Regular expressions have changed in Visual Studio 2013. https://msdn.microsoft.com/en-us/library/2k3te2cs(v=vs.120).aspx
To match an expression over two lines the code would now be:
StartOfExpression.*\r?\n.*EndOfExpression
Use (.*\n)*? to skip zero or more lines between your expressions.
start(.*\n)*?end
finds
start
two
three
end
? is the non-greedy operator, used to skip as few lines as possible.
If end is not the first word in the line, add .* to match the extra characters. I.e.: start(.*\n)*?.*end finds
start
two
three
four end end
If you only want to replace until the first end, add another non-greedy operator: start(.*\n)*?.*?end.
Historic: In Visual Studio 2017 (and early versions of 2019) you can also use the single line option in the Find and Replace dialog Ctrl-Shift-F like this:
(?s)start.*end
For more see the version history of this answer.
This works today in Visual Studio 2012:
fooPatternToStart.*(.*\n)+?.*barPatternToEnd
See how the (.*\n)+? part does the match across multiple lines, non-greedy.
fooPatternToStart is some regex pattern on your start line, while barPatternToEnd is your pattern to find on another line below, possibly many lines below...
Example found here.
Simple and effective :)
Note: before VS2012, the pattern that worked was: fooPatternToStart.(.\n)+#.*barPatternToEnd
Note: this answer is using the regex syntax used in Visual Studio up to and including VS 2012. In VS 2013 and later, the regex syntax has changed.
You can include \n in the expression. As an example, here is a regex that I use to "clean" auto-generated SQL scripts from anything that is not a stored procedure (it will match text blocks that start with a line containing "Object: " followed by something that is not "StoredProcedure", then matching the following lines up to a line consists of the word "GO"):
/\*+ Object\::b:b~(StoredProcedure)(.*\n)#GO\n
you may need to use \r\n at the end of your expression.
Non-greedy multi-line any character capture, Visual Studio 2013+:
.*?\r?\n.*?
Greedy version in Giles Roberts's answer.
For everyone coming here while searching for VS Code, I use this to match anything from script to anywhere with 2 newlines (newlines excluded):
script(.|\n)+?(?=\n\n)
replace script and \n\n to match everything between them.

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