Cleaning up xaml with regex - 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.

Related

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

Removing everything between a tag (including the tag itself) using Regex / Eclipse

I'm fairly new to figuring out how Regex works, but this one is just frustrating.
I have a massive XML document with a lot of <description>blahblahblah</description> tags. I want to basically remove any and all instances of <description></description>.
I'm using Eclipse and have tried a few examples of Regex I've found online, but nothing works.
<description>(.*?)</description>
Shouldn't that work?
EDIT:
Here is the actual code.
<description><![CDATA[<center><table><tr><th colspan='2' align='center'><em>Attributes</em></th></tr><tr bgcolor="#E3E3F3"><th>ID</th><td>308</td></tr></table></center>]]></description>
I'm not familiar with Eclipse, but I would expect its regex search facility to use Java's built-in regex flavor. You probably just need to check a box labeled "DOTALL" or "single-line" or something similar, or you can add the corresponding inline modifier to the regex:
(?s)<description>(.*?)</description>
That will allow the . to match newlines, which it doesn't by default.
EDIT: This is assuming there are newlines within the <description> element, which is the only reason I can think of why your regex wouldn't work. I'm also assuming you really are doing a regex search; is that automatic in Eclipse, or do you have to choose between regex and literal searching?

Search and replace in VS2008 - linebreak

If I need to replace a text
<p>
this text including the paragraph needs to be replaced
</p>
How can I do this with VS2008 "search and replace"?
EDIT
One way is to use regex like suggested by Daniel. Its just pretty complicated. The real searchexpression at the end was:
\<div id="searchStore"\>\n[^\<]*\<[^\>]*\>\n[^\<]*\<[^\>]*\>
Thats to much for simple minded persons like us.
Check the "Use Regular Expressions" checkbox in the Search/Replace dialog box and create a Regular Expression to match what you need.
(<p>\r\nthis text including the paragraph needs to be replaced\r\n</p>)
would match your example.