What Is a Tagged Expression?

What Is a Tagged Expression? thumbnail
Keyboard special characters make tagged expressions work.

Virtually every programming language -- and many third-party coding tools -- provide find-and-replace options that work with text strings. One that many provide is a common programming feature called a regular expression. While overall functionality and the specifics of how regular expressions work depends on the programming language or tool, all use them to search for, find and replace text. Tagged regular expressions, common in Microsoft Visual Studio applications, are an example of a regular expression function.

  1. Identification

    • Tagged regular expressions, more commonly referred to as tagged expressions, function as parameters in the “find” portion of a find-and-replace regular expression command. In a tagged expression, identifiers such as braces or brackets surround one or more sections of a text string you want to find, preserve and then incorporate into the replacement text. The total number of tagged expressions you can include in a single “find” command depends, however, on the programming language or tool. For example, in Visual Studio applications, you can include up to nine tagged expressions in each “find” command.

    Facts

    • The more complex the “fix,” the more useful tagged expressions become. A simple find-and-replace command is more appropriate when, for example, there are multiple occurrences of a single, identical formatting error, such as an extra space between an identifier and a colon. Tagged expressions are the better choice, however, when you have at least one instance of correctly formatted text as well as text strings containing multiple errors, such as a misspelled word and an extra space between an identifier and a colon. Tagged expressions become even more useful when there are multiple but differing errors, such as some text strings with one extra space and some with two extra spaces between an identifier and a colon. Here, tagged expressions allow you to take care of all errors in a single “find” command, using very little code.

    Example

    • A simple example of how you can use a tagged expression is to append the text “for your time” to every instance of text strings that include the words “Thanks” and “Thank you” using the Visual Studio 2010 Expression Builder. Open the “Find and Replace” dialog box from the VS main menu or via the keyboard command “Ctrl-Shift-H.” Open the Expression Builder by selecting “Regular Expressions” from the drop-down list in the Use section. Surround the regular expression “Thank[a-z]+” with braces in the “Find What” field to make it a tagged expression:

      {Thank[a-z]+}

      Add “\1 for your time” in the “Replace” field to append new text to all instances of the text string in “Tagged Expression 1.”

    Considerations

    • The syntax symbol for a tagged expression may be simple, but the brackets or braces you use make up only a portion of the syntax symbols a regular expression “find” command uses. While tools such as Visual Studio Expression Builder can make creating regular -- and tagged -- expressions easier, the complex syntax and formatting requirements of regular expressions make background information an essential prerequisite to formatting and using them correctly.

Related Searches:

References

Resources

  • Photo Credit Jupiterimages/Photos.com/Getty Images

Comments

Related Ads

Featured