SilverAge Software

Search and Replace. Edit. Transform.

Use Cases

How can I find and replace text inside a tag?

Question

How can I find text inside some tag and replace it with some other text? for e.g. if I want to replace something within this tag: <first> this is a test </first>.

I want the output which is <first> something </first>.

Answer

The solution is very simple. First, you have to match an opening tag:

\<tag_name[^\>]#\>

Here, [^\>]# matches any text after the tag name (attributes, values) until the closing > is reached.

Then, match tag inner text:

[^\<]#

which matches the tag inner text until the opening < is reached.

And the closing tag:

\<\/tag_name\>

The full search expression with () to store tags:

(\<tag_name[^\>]#\>)[^\<]#(\<\/tag_name\>)

Now, the replace expression:

\1something\2
  1. Type the search expression (\<tag_name[^\>]#\>)[^\<]#(\<\/tag_name\>) in the Find What field.
  2. Type the text that will replace the match in the Replace With field: \1something\2.
    To insert multiple lines of text, use the multiline editor by clicking the Replace With button.
  3. Check the Regular Expressions box and uncheck the Force search only box.
  4. Click Replace.