次の方法で共有


Replace in Files: Tagged Expressions

Keyboard:  CTRL + SHIFT + H
Menu:  Edit -> Find and Replace -> Replace in Files
Command:  Edit.ReplaceinFiles
Versions:  2008,2010
Published:  9/12/2010
Code:  vstipFind0016

 

Make sure to check out the Windows Phone Developer Portal!

 

As mentioned in vstipFind0015 (https://blogs.msdn.com/b/zainnab/archive/2010/09/11/replace-in-files-basic-options-vstipfind0015.aspx), the "Replace with" area is the most interesting piece of the Replace in Files dialog.  It can be as simple as a literal string replacement or quite complex.

image_thumb[1]

 

 

  Under normal situations this is just the literal text you want to use as a replacement to the "Find what" text.  However, when you choose to use regular expressions:

 image_thumb[2]

 

 

This will enable the Expression Builder:

image_thumb[3]

 

 

These options are NOT like the builder options you get in the "Find what" area:

image_thumb[5]

 

 

In addition to being able to use any of the normal regular expression characters, you can refer to the original text and any tagged expressions.  The best way to show how tagged expressions work is with an example.  Let's assume I have this text:

image_thumb[6]

 

And I use these options:

image_thumb[7]

 

Note the curly brackets around "jones"?  That denotes a tagged expression.  Every time you use { } it creates a tagged expression that is numbered (beginning with 1).  So, in this example we are looking for "bubba{jones}" and replacing with tagged expression 1 (which is just "jones").  Also, notice the notation used to refer to the tagged expression:  \n where n is the tagged expression we want. 

 

When I do my replacements, this is the result:

image

 

 

We can take this further.  If I had my original text:

image_thumb[6]

 

 

And want to turn "bubbajones" into "jonesbubba" then I could use these settings:

image

 

So now we are looking for "{bubba}{jones}" which will create tagged expression 1 ("bubba") and tagged expression 2 ("jones").  At this point, it's simply a matter of replacing with the expressions switched around "\2\1" and we get:

image

 

Of course, your finds and replaces can get way, way more complex than this when using Regular Expressions so if you are not familiar with regular expressions you can check out my series on them here:  https://blogs.msdn.com/b/zainnab/archive/2008/03/11/regular-expression-webcast-series.aspx.  There are some slight variations in the .NET Regular Expressions and the ones use here but this will help you get off the ground.  Good luck!

Comments

  • Anonymous
    August 01, 2013
    Thank you. The tagges expressions was exactly what I needed!  :)