DataGrip 2018.3 Help

Find and replace a string 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.

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

  2. Select the Regex checkbox to enable the regex syntax. If you need to check the regex syntax reference, click the question mark next to the Regex.

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

    Regex search and replace fields
  4. When you 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.

  5. 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]
  6. If the Match Case checkbox is cleared, DataGrip searches for both lower and upper cases. Select the Match Case checkbox to match the case of the specified range.

    The result of the Match Case selection

  7. When you browse the occurrences, DataGrip displays the replacement hints so you can view the potential results before you press 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 (back reference) them in your replace pattern. Note that the group 0 refers to the entire regular expression.

Let's consider the following code:

<new product="ij" category="105" title="Multiline search and replace in the current file"/> <new product="ij" category="105" title="Improved search and replace in the current file"/> <new product="ij" category="105" title="Regexp shows replacement preview"/> ...
  1. Open the search and replace pane (Ctrl+R).

  2. In the search field, enter parentheses () that would indicate a capturing group, for example:

    \stitle="(.*)?"\s*(/>*)

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

    $2<title>$1</title>

  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 (Ctrl+R). Make sure that the Regex checkbox is selected.

  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.

The following SQL code fragment is used as an example for this tutorial.

insert into employee (emp_id, First_Name, Last_Name, start_date, dept_id, title, assigned_branch_id) values (null, 'Michael', 'Smith', '2001-06-22', (select dept_id from DEPARTMENT where name = 'Administration'), 'President', (select branch_id from branch where name = 'Headquarters')); insert into employee (emp_id, First_Name, Last_Name, start_date, dept_id, title, assigned_branch_id) values (null, 'Susan', 'Barker', '2002-09-12', (select dept_id from DEPARTMENT where name = 'Accounting'), 'Vice President', (select branch_id from branch where name = 'Branch')); ...

Find and replace a string

  1. Open an SQL file with the example code, press Ctrl+R. The Replace pane appears at the top of the code editing window.

  2. Select the Regex checkbox.

  3. In the search field, type the following regular expression:

    \(select dept.*?= (.*)\)

  4. In the Replace field, type $1 (for more information, see capture groups).

  5. Click Replace, or Replace All.

Example of a regular expression

Changing character case

Description

Regular expression for the replace field

Result

Change to upper case

\U$1'ADMINISTRATION'

Change to lower case

\L$1'administration'

Change the first letter to lower case

\l$1'administration'
Last modified: 28 November 2018

See Also