DataGrip 2023.3 Help

Find and replace text using regular expressions

When you want to search and replace specific patterns of text, use regular expressions. They can help you in pattern matching, parsing, filtering of results, and so on. Once you learn the regex syntax, you can use it for almost any language.

The IDE uses Java Regular Expressions, which are the regular expressions included in the JDK that the IDE runs on. For more information about patterns, please refer to Class Pattern at docs.oracle.com. These expressions are mostly, but not entirely, PCRE (Perl Compatible Regular Expressions) compatible.

  1. Press Control+R to open the search and replace pane.

  2. Enter a search string in the top field and a replace string in the bottom field.

    Regex search and replace fields

    Click the Regex icon to enable regular expressions. If you want to check the syntax of regular expressions, hover over the Regex icon and click the Show expressions help link.

  3. When you search for a text string that contains special regex symbols, DataGrip automatically escapes them with backlash \ in the search field.

    Escape regex symbols

    However, when you specifically search for metacharacters such as .[{()\^$|?*+, you need to escape them with backslash \, so they can be recognized.

    For example, if you need to find ., type \. in the search field.

  4. DataGrip can also match a letter case when you enter a range of characters in your search field.

    For example, if you want to search for only uppercase characters, type the following in the search field:

    \b[A-Z]

    To search and replace more complicated patterns, use the structural search and replace.

  5. If the Match Case icon is unselected in the search field, DataGrip searches for both lower and upper cases.

    Select the Match Case icon to match the case of the specified range.

    The result of the Match Case selection
  6. When you browse the occurrences, DataGrip displays the replacement hints, so you can view the potential results before clicking the Replace button.

    Replacement hints

Use regex capturing groups and backreferences

You can put the regular expressions inside brackets in order to group them. Each group has a number starting with 1, so you can refer to (backreference) them in your replace pattern. Note that the group 0 refers to the entire regular expression. However, you can refer to the captured group not only by a number $n, but also by a name ${name}.

For example, for the numbered capturing groups, use the following syntax:

<h2>(.*?)</h2>
$1
Numbered regex capturing group

For the named capturing groups, use the following syntax:

<h2>(?<title>.*?)</h2>
${title}
Named regex capturing group

Find and replace a captured group

Let's consider the following SQL statements:

Insert into (country_id,country,last_update) Values ('26','Czech Republic','2006-02-15 04:44:00.000'); Insert into (country_id,country,last_update) Values ('27','Dominican Republic','2006-02-15 04:44:00.000'); Insert into (country_id,country,last_update) Values ('28','Ecuador','2006-02-15 04:44:00.000');

  1. Open the search and replace pane Control+R.

  2. In the search field, enter parentheses () that would indicate a capturing group, for example: into\s(\((.*)\W(.*)\W(.*)\))$.

  3. In the replace field, backreference such groups by numbers starting with 1, for example: into $3 $1.

  4. DataGrip highlights the found occurrences based on your search specifications and displays hints with the replace string.

    Replace with regex result

Switch the character case

You can use regular expressions to change the case of characters that matches some criteria.

  1. Open the search and replace pane Control+R. Make sure that the Regex icon is selected in the search field.

  2. In the search field enter the search pattern.

  3. In the replace field, depending on what you want to achieve, enter one of the following syntax:

    • \l changes a character to lowercase until the next character in the string.

      For example, Bar becomes bar.

    • \u changes a character to uppercase until the next character in the string.

      For example, bar becomes Bar.

    • \L changes characters to lowercase until the end of the literal string \E.

      For example, BAR becomes bar.

    • \U changes characters to uppercase until the end of the literal string \E.

      For example, bar becomes BAR.

    Switch to the uppercase character example
Last modified: 13 December 2023