Structural search and replace examples
As you know the main difference between regular search and the structural search is that in the structural search we are looking for a structural template in a programming language.
The beauty of a structural search is that you can create a pattern based on the existing template and save yourself time when searching and replacing code.
The extensive list of existing templates covers a lot of use-cases from simple patterns to more complex ones.
Each item in a pattern consists of variables that are limited by $ sign on both sides.
The following examples show how you can use structural search in HTML and XML code.
The simplest template to search for a tag is <$tag$/>
.
By placing constraints on the variable
$tag$
, you can specify tags that you want to find. For example, if you specifyli
, you will get allli
tags.Consider the following template for searching in XML and HTML:
<$tag$ $attribute$=$value$ />
. For example, if you specify the text filterid
for the$attribute$
variable and the\d+
regular expression as the text filter for the$value$
variable, you can find all tags that have numeric values in theid
attribute.
Create an HTML file and paste the following code:
HTML code
{...}In the main menu, go to Edit | Find | Search Structurally.
From the Language list, select HTML.
Paste the following string to the Search template field:
<$tag$ $attribute$="$value$">
Click the
$tag$
variable.In the filter panel, click Add modifier, select Text and type
li
in the value field.If the filter panel is not visible, click Toggle Modifier Panel.
Click the
$attribute$
variable.In the filter panel, click Add modifier, select Text and type
id
in the value field.Click the
$value$
variable.In the filter panel, click Add modifier, select Text and type
\d+
in the value field.The
\d+
regular expression limits search results to numeric values. So, the line with theid="a"
will be filtered out.Without switching the focus from the filter panel, click the Add button, select Script and paste the following code:
value.getText().replaceAll (/"/, '').toInteger() > 2
The script reads the content of the
$value$
variable and returns it as a string (for example,"1"
). Then the script replaces all the quotes and converts the string value to integer and compares it with2
.
note
To switch between different selection scopes, select the necessary value from the Target list. For example, to select the whole line that matches the template, select Complete match.
Create an HTML file and paste the following code:
HTML code
{...}In the main menu, go to Edit | Find | Replace Structurally.
From the Language list, select HTML.
Paste the following string to the Search template field:
<$tag$ $attribute$="$value$">
Select the Match case checkbox.
Click the
$tag$
variable.In the filter panel, click Add modifier, select Text and type
li
in the value field.Click the
$attribute$
variable.In the filter panel, click Add modifier, select Text and type
class
in the value field.Click the
$value$
variable.In the filter panel, click Add modifier, select Text and type
[A-Z].*
in the value field.The
[A-Z].*
regular expression limits search results to uppercase values.note
Sometimes you might need to use inline flags. In this case the regular expression will be the following:
(?-i)\b[A-Z]+\b
.(?-i)
ensures that case-insensitive mode is turned off.From the Target list, select value. This procedure highlights all the uppercase values of the
class
attribute.In the Replace template field, paste the
$to_lower_case$
variable.Click the
$to_lower_case$
variable.In the filter panel, click Add modifier, select Script and paste the following code:
value.getText().toLowerCase()
Click Find.
In the Find tool window, preview the found results and click Replace All.
Thanks for your feedback!