0

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>
Community
  • 1
  • 1
Ryan Gates
  • 4,501
  • 6
  • 50
  • 90

2 Answers2

1

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)

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • Unfortunately VS 2012 regex doesn't seem to like it. Do you know of another editor or script that this will work for? – Ryan Gates Jun 27 '13 at 16:06
  • @RyanGates: I suggest you to test these patterns with notepad++ – Casimir et Hippolyte Jun 27 '13 at 16:23
  • Thank you. This worked for me with Notepad++ 6.3.2 (UNICODE). – Ryan Gates Jun 27 '13 at 17:48
  • Notepad++ Uses a regular Regex Engine. Visual Studio has a special engine for it's search/replace matching. Please see my example. My example will not work in Notepad or RegexBuddy or anywhere else except Visual Studio Search/Replace... which as I understand it is where you need it. – Suamere Jun 27 '13 at 18:05
0

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.

Suamere
  • 5,691
  • 2
  • 44
  • 58