Find and replace in files - .NET friendly regular expression syntax - regex

I'm looking for a decent tool that can do search and replace over multiple files, with regular expression syntax I'm use to in C#. Normally I would do this in Visual Studio, except it has the strangest regex syntax (and this is meant to be faster than just replacing the text in the files manually).
So far I've tried windows grep but it didn't like the regex below. The regex in question is
<see cref="(?<class>.+)">(.+)</see>
To replace with
<see cref="${class}"/>
Alternatively converting this to Visual Studio's syntax would be fine!
Jeff has a whole post on this on his blog.

In Visual Studio, find:
\<see cref="{:i}"\>.*\</see\>
and replace with:
<see cref="\1"/>
{} is the VS grouping operator. It is not named. In replacements \n is used for the value of the n-th group in the find expression.
You can tweak the :i and .* parts if you need to account for new lines or other whitespace.

In Visual Studio you can do this with the following Reg Ex:
\<see cref="{.+}"\>.+\</see\>
Replace:
<see cref="\1" />
Remember to select Use/Regular Expressions in the Find and Replace dialog.
I've long been thinking it sucks that Visual Studio does not support the same Regular Expression syntax as the .Net framework.

There are a few choices:
If it's something you do regularly, I would go with PowerGrep as it is probably the most complete and user-friendly tool for this.
Its sibling, RegexBuddy also has a Grep functionality, albeit not as comprehensive.
If you're not afraid of Perl, you can also use its command line as a search and replace tool.
Otherwise, there is still the venerable sed that works on Windows.

notepad++ is quite popular and lightweight and has this functionality, have you tried that?:
http://notepad-plus.sourceforge.net/uk/site.htm
EDIT:
It would appear my link is outdated and this usage is only quite recent:
http://www.opensourcereleasefeed.com/release/show/notepad-v5-2-released-replace-in-files-feature-added

You can use the .NET Regular Expression AddIn to allow you to use .NET regexes with Visual Studio. It's on CodeProject at http://www.codeproject.com/KB/macros/VS2005RegexAddIn.aspx.
Mike

Related

What flavor of Regex does Visual Studio Code use?

Trying to search-replace in Visual Studio Code, I find that its Regex flavor is different from full Visual Studio. Specifically, I try to declare a named group with string (?<p>[\w]+) which works in Visual Studio but not in Visual Studio Code. It'll complain with the error Invalid group.
Apart from solving this specific issue, I'm looking for information about the flavor of Regexes in Visual Studio Code and where to find documentation about it, so I can help myself with any other questions I might stumble upon.
Full Visual Studio uses .NET Regular Expressions as documented here. This link is mentioned as the documentation for VS Code elsewhere on Stackoverflow, but it's not.
Rust Regex in the Find/Replace in Files Sidebar
Rob Lourens of MSFT wrote that the file search uses Rust regex. The Rust language documentation describes the syntax.
JavaScript Regex in the Find/Replace in File Widget
Alexandru Dima of MSFT wrote that the find widget uses JavaScript regex. As Wicktor commented, ECMAScript 5's documentation describes the syntax. So does the MDN JavaScript Regular Expression Guide.
Test the Difference
The find in files sidebar does not support (?=foobar) whereas the find in file widget does support that lookahead syntax.
Regarding Find/Replace with Groups
To find/replace with groups, use parentheses () to group and $1, $2, $3, $n to replace.
Here is an example.
Before:
After:
Shaun's answer is still correct, however to add an update, recently VS Code added the option to opt into using the Perl based PCRE2 engine. You can enable this through your settings config.
This allows you to perform more advanced regex operations like lookaheads and backreferences. But as noted below, the regex still has to be valid JavaScript regex.
VS Code does support regular expression searches, however,
backreferences and lookaround aren't supported by default. But you can
enable these with the setting search.usePCRE2. This configures ripgrep
to use the PCRE2 regex engine. While PCRE2 supports many other
features, we only support regex expressions that are still valid in
JavaScript, because open editors are still searched using the editor's
JavaScript-based search.
And for a bonus if you ended up here trying to do multi line searches, VS Code recently added that feature as well!
I've found newer information (July 22, 2020) about it.
IllusionMH left the following comment in Github:
ripgrep (compatible with PCRE2) is already used for Find in files
functionality (for not open editors) and JS engine used only for open
editors.
Which regex engine does vscode use? It is now a little more nuanced than previously. The best source is this vscode wiki: Github wiki: Notes on Regular Expression Support:
[At top of document]
This document applies to search (CMD+SHIFT+F/CTRL+SHIFT+F) and
quickopen (CMD+P/CTRL+P). By default, VS Code uses the ripgrep tool to
drive search.
...
[At end of document]
Text search uses two different sets of regular expression engines. The
workspace is searched using ripgrep, which will use the Rust regex
engine, and will fallback to PCRE2 if the regex fails to parse in the
Rust regex engine. The Rust regex engine doesn't support some features
like backreferences and look-around, so if you use those features,
PCRE2 will be used. Open files are searched using a JS regex in the
editor itself. Most of the time, you don't need to worry about this,
but you may see an inconsistency in how some complex regexes are
interpreted, and this can be an explanation. Especially when you see a
regex interpreted one way when a file is open, and another way when it
is not. During a Replace operation, each file will be opened in turn,
and the search query will be run as a JS regex.
Another potential issue is how newlines are handled between ripgrep
and the editor. The editor normalizes newlines, so that you can match
both CRLF and LF line endings just with \n. It's actually not possible
to match \r explicitly in the editor because it is normalized away.
When searching in the workspace, VS Code tries to rewrite a regex so
that \n will match CRLF. But \r\n or \s\n will also match CRLF in
closed files, but not in open files.
Two key points: (1) newlines are handled specially and (2) backrefereences and look-arounds are supported despite using the Rust regex engine - if your regex has a look-around or backreference in it PCRE2 will be used instead of the Rust engine.
More on lookarounds
The Find Widget (Ctrl+F) used for finding within the active editor only supports all lookarounds (lookahead and lookbehind) and those lookarounds can be non-fixed-length. So this will work in the Find Widget: (?<!blah.*).
In a search across files (Ctrl+Shift+F) non-fixed-length lookbehinds DO NOT WORK. Lookaheads can be fixed or non-fixed-length. But non-fixed-length positive or negative lookbehinds do not work and you will get an error message below the search input box: Regex parse error: lookbehind assertion is not fixed length which may not appear until you actually try to run the search.

How can I fix this regex in Visual Studio 2010?

I'm trying to search using this regex in Visual Studio, but it doesn't work.
<(script|link)\b(.*?)Site.css(.*?)(script>|/>)
After reading this article I changed it to
\<(script|link)\b(.*?)Site.css(.*?)(script\>|/\>)
But it still doesn't work. What am i doing wrong?
Your help is much appreciated :)
look here...it's quite old now but nice to read http://www.codinghorror.com/blog/2006/07/the-visual-studio-ide-and-regular-expressions.html
Following the guide above I tried to translate your regular expression. Unforunately I couldn't find anything suitable for the non-greedy quantifier *? and was forced to stick with the greedy one *. That's my guess (successfully tested on visual studio 2010):
\<{script|link}.+Site.css.*{/\>|script\>}
I feel to suggest you a different editor when you want to intensively use regular expressions on Visual Studio resource files like xml stuff. My personal choice was PowerGrep but that's a commercial product. I'm sure there are lots of them for free on the internet.

Visual Studio 2010: pattern based search replace other than regex?

I've taken over a project that is full of code like this:
if (aTraceUserids[t].Trim().ToUpper() == Userid().Trim().ToUpper())
{
// ...
}
What is - using tool-assisted expression formulation - a good way to do a search replace into something like this on a case by case base:
if (aTraceUserids[t].Equals(Userid(), StringComparison.InvariantCultureIgnoreCase))
{
// ...
}
Edit (thanks Dave for making me think on this further):
I know this should be possible with regular expressions, but those are hard to get right and document, so I wonder about tool assisted ways that help me both phrase the expressions and execute them.
Ideally I'm looking for a pattern based search/replace tool that allows me to
enter the search/replace patterns
enter the patterns for the files and directory names to match
visually assists me with the search/replace matches, and allows me to post-edit each occurrence
I don't care much which platform as these kinds of search/replace actions will likely apply to other big code bases as well.
So: any solution based on *nix, Windows or web are fine. CygWin and/or WINE based solutions are fine too. (That's why I removed the VS2010 tag and added some platform tags).
Since this was originally tagged with 'Visual Studio', Visual Studio itself can do regular-expression based find/replace, and the standard 'Find and Replace' dialog will let you pick and choose by hitting 'Find Next', 'Replace' or 'Replace All' as you choose.
For example, I recently changed an API from;
Log.Error(string message, bool someotherArg);
to
Log.Error(string message);
And easily used Visual Studio to replace all usage throughout my codebase of this modified API like so;
Find what;
Log.Error({.*}, true);
Replace with:
Log.Error(\1);
The backquote in the replace line \1 puts the grouped regex contained by {...} into that spot in the replace.
Handy, and built-in. Works for me.

Visual Studio Find and Replace with Regex

I want to replace C# attributes with VB.NET, which means, [Serializable] should become <Serializable>.
The pattern (\[)(.+)(\]) does find the results but I don't know how to replace the first and the last groups with the appropriate parenthesis.
I read this page, but I didn't understand how to use the curly braces for F&R, I tried to wrap the groups with it but it didn't work.
If you are using the Productivity Power Tools extension from Microsoft that support normal .NET regexes, what you would put in the textbox for the replacement given your regular expression above is:
<$2>
where $2 refers to the second capture group in your regex, i.e. the text between the brackets.
Note that this only works with the Quick Find from Productivity Power Tools though. The normal find/replace in Visual Studio use another syntax altogether.
Find what: \[{Serializable}\]
Replace with: <\1>

Visual Studio 2008 search and replace regex

I have a large solution with a lot of lines that I need to replace.
In Visual Studio, you can search and replace with the aid of regular expressions.
I want to replace lines like:
rst.Fields("CustomerName").Value
rst.Fields("Address").Value
rst.Fields("Invoice").Value
To:
row("CustomerName").ToString()
row("Address").ToString()
row("Invoice").ToString()
Thus keeping the dynamic text part, which can vary.
Is this possible and how?
Update, solution:
Search: rst.Fields{\(.*\)}\.Value
Replace: rst\1.ToString()
Thanks JaredPar!
Try the following
Search Expression: ASpecificCommand(\(.*\))\.ASpecificProperty
Replace Expression: ATotallyDifferentCommand\1.ATotallyDifferentProperty
Note: This is not a perfect solution. Since there are (s involved and hence matching of nested parens, a regex won't ever be a perfect solution. However it should get the job done for the specific pattern you posted
The answer and solution provided helpful in doing a find-replace on messageboxes.
This worked in Visual Studio 2008 (VB .NET):
Example:
MessageBox.Show("Invalid Entry","Error")
Find What:
MessageBox.Show{(.*,*)}
Replace WIth:
Error.ShowError\1\2
Results in:
Error.ShowError("Invalid Entry","Error")
Looks like you have it nailed. It's what is called a "tagged expression" and you can see another example here:
http://blogs.msdn.com/b/zainnab/archive/2010/09/12/replace-in-files-tagged-expressions-vstipfind0016.aspx