Using Find and Replace/Regular Expressions to Change Opening and Closing Tags

 Article #: GEN1016Z  Product: Flare   Version: All

Summary:

Below are the steps for using Regular Expressions in conjunction with Find and Replace to change opening and closing tags while maintaining the content inside of the tags.

Full Description:

When trying to replace an element with a entirely new element using a simple Find and Replace does not work effectively because we want to maintain the content in between tags as well as replace the closing tag. Regular expressions (also referred to as regex or regexp) are similar to wildcards in that they let you find matching strings of text. However, they are more powerful than wildcards. By using a regular expression we can change both the opening and closing tags while preserving the information between the tags.

In this example we want to change the source code below:

from this

<p class="heading1">Hello MadWorld!</p>

to this

<h1>Hello MadWorld!</h1>


  1. Navigate to the "Home" ribbon menu and select "Find and replace in Files"
  2. In the Find field insert:
  3. <p class="heading1">((.?\s?)*?)</p>

  4. In the Replace with field insert "\1" with the desired opening and closing tags on either side:
  5. <h1>\1</h1>

  6. Under Find Options enable "Find in source code" and set Search Type to "Regular Expressions"
  7. Perform the find and replace and confirm that the tags have changed appropriately via the XML Editor

Additional Information

The "\1" used in the "Replace with" field in step 3 is called a backreference, this is used to match the text in the first logical grouping in the regular expression in the "Find" field in step 2. The first logical grouping, in this case, is everything within the outermost parentheses. This is what tells the find and replace to maintain the text within the xml tags.

For more information on regular expressions please visit the resources below.

http://www.regular-expressions.info/

http://regexlib.com/CheatSheet.aspx

Attribution:

Last updated:

September 30, 2016

Author:

Steven Moreno

Contributions by:

Justin Bondoc