Beginner’s tips on VS Find and Replace using Regular Expressions – Tutorial #3

In answering some blog posts, I mentioned tagged expressions.  I thought I'd talk about them now before going on to other regular expression syntax.

Tagged expressions are useful for doing replacements in a regular expression search.  When you tag an expression in the find what field, that expression can be used as part of the replace text.  In a single find/replace, you can have up to 9 tagged expressions.  Let's move on to the following example:

Hello sun!  Heya moon!  Hiya world!

Now, let's assume that you want to search for all occurances of “Hello”, “Heya“, and “Hiya“, and attach a “to the“ after each word, therefore replacing each respectively with “Hello to the”, “Heya to the“, “Hiya to the“.  To find “Hello, “Heya”, and “Hiya”, we can use the search criteria H[a-z]+.  To tag our search criteria, we place {} around the criteria.  Therefore, we will put {H[a-z]+} in the find what field.  In the replace field, since we want our match to appear in the replace text, we'll use \1 to signify the first tagged expression.  Therefore, our replace text will be \1 to the.

After this find/replace, you will then end up with:

Hello to the sun! Heya to the moon! Hiya to the world!

As I mentioned, you can up to 9 tagged expressions in one search.  Also, the replace syntax \0 means to use the entire matched text.  Therefore, we could have used \0 to the as our replace criteria.

Now, on to more regular expression syntax (recall that clicking the expression builder button beside the find what/replace with combos will drop down a list of commonly used regular expressions and also a link to the complete regular expressions syntax help topic.  Note that if you selected “Wildcard“ under “Use:“ in the Find Options, then the syntax/help link for Wildcard will be shown instead.

There are some great shortcuts so that you don't always need to specify a range.  For example, :a matches any alphanumeric character ([a-zA-Z0-9]), :c matches any alphabetic character ([a-zA-Z]), :d matches any decimal digit ([0-9]), :w matches any alphabetic string ([a-zA-Z]+), and :z matches any decimal integer ([0-9]+)

There are also other shortcuts such as :Lu matches any upper-case letter, :Ll matches any lower-case letter.  The complete list can be found in the Regular Expression help topic, and I encourage you all to browse through that lists, and if you have any questions, send them this way.




Skip to main content