Parentheses in regular expression (RegEx) can be used for grouping expression.
Within the same expression you can backreference the group using the following:
\<GROUP_POSITION>
<GROUP_POSITION> is the quantitative location of the group from left to right starting from 1.
On the replace field, it follows the <GROUP_POSITION> but instead of using backslash (i.e. \) use dollar sign (i.e. $) to precedes it.
Note: if your group exceeds a single digit use ${<nn>} (e.g. ${10}) notation.
Example
<?xml version="1.0"?> <fruits> <a>apple</a> <b></b> <c>cashew</c> <d></d> </fruits>
From the XML above find all the empty elements and add an attribute empty that is set to true.
Find | Replace | Comment |
<(\w*[^>])></\1> | <$1 empty="true"></$1> | The group (i.e. blue text) is assigned to $1.
The \1 (i.e. green text) is the backreference. |
Leave a Reply