Visual Studio Find and Replace with Regex - 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>

Related

Regex for form.controls['property']

I am trying to find replace thousands of strings in code - form.controls['property'] with form.get('property') where property is a variable.
So far I got find expression .controls\['\w+'\] with replace expression .get\('\w+'\) but VSCode is replacing .controls['products'] with .get\('\w+'\) for string productForm.controls['products'].controls.length. I need to replace it with productForm.get('products').controls.length Any ideas how to fix it? Thanks!
Parentheses () capture a group. Money sign $ accesses a group by index.
.controls\['(\w+)'\]
.get('$1')
This is the result in VS Code:
See also: What flavor of Regex does Visual Studio Code use?

Dreamweaver regexp positive look behind error

I'm getting a syntax error and invalid quantifier error in dreamweaver when I try to use a regexp in the source code.
The purpose is to find spaces in front of numbers on table cells and delete them.
(?<=>)\s+(?=\d)
this expression works on notepad++ but not in dreamweaver.
Can this be a Dreamweaver bug or the syntax is wrong?
Of course I can make a text search looking for >\s and replace by > but then I cant catch more spaces than the ones specified in the search string
thanks in advance
PS: Would be nice also to have a multisearch option in the dreamweaver search screen, to run multiple search and replace in one operation, like code clean up. An extension maybe?
I don't use DW but, since I have read several posts about lookarounds problems with DW, I assume that DW doesn't support these regex features.
You can use capturing groups instead (if DW supports it!):
search : (>)\s+(\d)
replace: $1$2
or
replace: \1\2
To append to the previous answer, when formulating a replace statement in DreamWeaver, use the format of $1 rather than ^1 for the variables.
I receive a similar "invalid quantifier" response in DreamWeaver CC 2015.1 when using a negative lookbehind:
(?<!somephrase)

Cleaning up xaml with regex

I am working on some xaml that is cluttered. The Label's have their own name and TabIndex, which are both unnecessary in my situation. Take the following example Label element,
<Label Canvas.Left="20" Canvas.Top="40" x:Name="Page2Label15" Width="40" TabIndex="40">Great Label</Label>
How can I use Visual Studio 2012 (or another xaml editor) to remove the unwanted name and TabIndex attributes without targeting false positives? By false positives, I mean something like the below that I need to preserve.
<TextBox Canvas.Left="20" Canvas.Top="40" x:Name="Page2Textbox15" Width="70" TabIndex="40">Great Label</TextBox>
It seems like this should be possible, because the VS 2012 Find and Replace allows for the use of regular expressions. I have had trouble getting the look around regular expressions to work correctly.
I would like the end result to be:
<Label Canvas.Left="20" Canvas.Top="40" Width="40">Great Label</Label>
I don't know the regex capabilities of visual studio, but you can try this search/replace:
search: (<Label [^>]+?)(?:(?:x:Name|TabIndex)="[^"]*")([^>]*?)(?:(?:x:Name|TabIndex)="[^"]*")([^>]*>)
replace: $1$2$3 or \1\2\3
The idea is to capture all the content of the label tags that are not attributes you want to remove and then replace only with the captured strings.
EDIT: it seems that this pattern works only when the two ugly attributes are present.
You can remove the cases with an only one attribute with:
search: (<Label [^>]+?)(?:(?:x:Name|TabIndex)="[^"]*")
replace: $1
(or use this only pattern twice)
Notice: You can try these patterns with notepad++ (take the last version)
Visual Studio uses a very special Regular Expression engine. I'm not talking about the .Net Framework, I mean the actual VS GUI. It's sorta gross.
Visual Studio Regex Example
Anyway... try something like this...
{\<Label[^\>]*}(x\:Name|TabIndex)=:q
Replace with \1
You'd have to run it twice.
If I read correctly, :q should match everything in quotes, including the quotes. Also, you can use Parenthesis as sub-expressions, but Capture-Groups are actually with Curly-Braces, which are typically quantifiers in normal Regex. Very odd. Lastly, the Colon, and GT/LT symbols are special characters, so I had to escape all of those.

Regular Expression Searching Matching One Word but Not Another

<ReportExport ID="export1" runat="server" AlertNoTests="false" PDFPageOrientation="Portrait"
HideExcel="true" OnPDFClicked="CreatePDF" AllowPDFOptions="true" HideBulkPDFOptions="false"
HideOrientation="true" HidePaperSize="true" MaxReportsAtOnce="250" HideTextExport="true" />
I'm trying to use Visual Studio's find feature using regular expressions to find ReportExport in my entire solution where the HideTextExport property is not being set. This is only ever defined in the markup once on a given page.
Any ideas on how I would find where ReportExport exists... but HideTextExport does not exist in the text?
Thanks in advance!
This works for me:
\<ReportExport(:Wh+~(HideTextExport):w=:q)+:Wh*/\>
:Wh+ matches the whitespace preceding the attribute name and :w matches the name, but only after ~(HideTextExport) confirms that the name is not "HideTextExport". :q matches the attribute's value (assuming values are always quoted). < and > have to be escaped or VS Find will treat them as word boundaries.
This is effectively the same as the .NET regex,
<ReportExport(?:\s+(?!HideTextExport)[A-Za-z]+="[^"]+")+\s*/>
First off one should install the Productivity Power tools to Visual Studio (via Tools->Extension Manager) and use .net regex instead of the antiquated regex provided out of the box for the Visual Studio Find.
With that the user could use this regex pattern (if the productivity power tools has singleline turned on to handle the span of lines for the element):
(ReportExport.+?HideTextExport="false")
That will return all reportexports where its false and one could tweak the regex to change it to replace false to true.
But...if the HideTextExport is missing, this makes regex a poor choice to use to find this element because the uncertantity of the location of the attribute makes the .* or .+ too greedy and ends reporting false positives when trying to find a missing text in a match.
A generalized way of saying that is, regex finds patterns and that is its job, but it requires lexical analsys to find missing patterns where regex simply cannot.

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