# Find and replace text using regular expressions

When you want to search and replace specific patterns of text, use [regular expressions](https://www.regular-expressions.info/quickstart.html). 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](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/regex/Pattern.html). These expressions are mostly, but not entirely, PCRE (Perl Compatible Regular Expressions) compatible.

![Regex icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/show_regex_java.png)

Procedure:

1. Press `Ctrl+R` (Windows), `⌘ R` (macOS), `⌘ R` (IntelliJ IDEA Classic (macOS)), `⌘ R` (macOS System Shortcuts), `Ctrl+R` (XWin), `Ctrl+R` (GNOME), `Ctrl+R` (KDE), `Alt+Shift+5` (Emacs), `Ctrl+H` (Sublime Text), `⌘ ⌥ F` (Sublime Text (macOS)), `Ctrl+H` (NetBeans), `Ctrl+H` (Visual Studio), `⌃ H` (Visual Studio (macOS)), `Ctrl+F` (Eclipse), `Ctrl+R` (Eclipse (macOS)) to open the search and replace pane.

> **Note:**
> If you need to search and replace in more than one file, press `Ctrl+Shift+R` (Windows), `⌘ ⇧ R` (macOS), `⌃ ⇧ R` (IntelliJ IDEA Classic (macOS)), `⌘ ⇧ R` (macOS System Shortcuts), `Ctrl+Shift+R` (XWin), `Ctrl+Shift+R` (GNOME), `Ctrl+Shift+R` (KDE), `Ctrl+Shift+R` (Emacs), `Ctrl+Shift+R` (Sublime Text), `Ctrl+Shift+R` (Sublime Text (macOS)), `Ctrl+Shift+H` (NetBeans), `Ctrl+Shift+H` (Visual Studio), `⌘ ⇧ H` (Visual Studio (macOS)), `Ctrl+Shift+R` (Eclipse), `Ctrl+Shift+R` (Eclipse (macOS)).  For more information, refer to [Search and replace a target within a project](finding-and-replacing-text-in-project.html).

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

![Regex search and replace fields](https://resources.jetbrains.com.cn/help/img/idea/2026.2/regex_search_and_replace_fields.png)

Click ![the Regex icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.inline.regex.svg) to enable regular expressions. If you want to check the syntax of regular expressions, hover over ![the Regex icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.inline.regex.svg) and click the Show expressions help link.

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

> **Note:**
> Keep in mind that if you copy (`Ctrl+C` (Windows), `⌘ C` (macOS), `⌘ C` (IntelliJ IDEA Classic (macOS)), `⌘ C` (macOS System Shortcuts), `Ctrl+C` (XWin), `Ctrl+C` (GNOME), `Ctrl+C` (KDE), `Ctrl+Insert` (Emacs), `Ctrl+C` (Sublime Text), `⌘ C` (Sublime Text (macOS)), `Ctrl+C` (NetBeans), `Ctrl+C` (Visual Studio), `⌘ C` (Visual Studio (macOS)), `Ctrl+C` (Eclipse), `⌘ C` (Eclipse (macOS))) the string first and then paste (`Ctrl+V` (Windows), `⌘ V` (macOS), `⌘ V` (IntelliJ IDEA Classic (macOS)), `⌘ V` (macOS System Shortcuts), `Ctrl+V` (XWin), `Ctrl+V` (GNOME), `Ctrl+V` (KDE), `Shift+Insert` (Emacs), `Ctrl+Shift+V` (Sublime Text), `⌘ ⇧ V` (Sublime Text (macOS)), `Ctrl+V` (NetBeans), `Ctrl+V` (Visual Studio), `⌘ V` (Visual Studio (macOS)), `Ctrl+V` (Eclipse), `⌘ V` (Eclipse (macOS))) it in the search field, the regex symbols will not be taken into account.

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. IntelliJ IDEA 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](structural-search-and-replace.html).

5. If ![the Match Case icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.inline.matchCase.svg) is unselected in the search field, IntelliJ IDEA searches for both lower and upper cases.

Select ![the Match Case icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.inline.matchCase.svg) to match the case of the specified range.

![The result of the Match Case selection](https://resources.jetbrains.com.cn/help/img/idea/2026.2/match_case_regex.png)

6. When you browse the occurrences, IntelliJ IDEA displays the replacement hints, so you can view the potential results before clicking the Replace button.

![Replacement hints](https://resources.jetbrains.com.cn/help/img/idea/2026.2/replacement_hints.png)

> **Note:**
> See [RegEx syntax](regular-expressions.html) for more details.

## 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:

Find field:

```
<h2>(.*?)</h2>
```

Replace field:

```
$1
```

For the named capturing groups, use the following syntax:

Find field:

```
<h2>(?<title>.*?)</h2>
```

Replace field:

```
${title}
```

Procedure: Find and replace a captured group

Let's consider the following :

```XML
<new instance="ij" category="105" title="Multiline search and replace in the current file"/>
<new instance="ij" category="105" title="Improved search and replace in the current file"/>
<new instance="ij" category="105" title="Regexp shows replacement preview"/>
```

1. Open the search and replace pane `Ctrl+R` (Windows), `⌘ R` (macOS), `⌘ R` (IntelliJ IDEA Classic (macOS)), `⌘ R` (macOS System Shortcuts), `Ctrl+R` (XWin), `Ctrl+R` (GNOME), `Ctrl+R` (KDE), `Alt+Shift+5` (Emacs), `Ctrl+H` (Sublime Text), `⌘ ⌥ F` (Sublime Text (macOS)), `Ctrl+H` (NetBeans), `Ctrl+H` (Visual Studio), `⌃ H` (Visual Studio (macOS)), `Ctrl+F` (Eclipse), `Ctrl+R` (Eclipse (macOS)).

2. In the search field, enter parentheses `()` that would indicate a [capturing group](https://www.regular-expressions.info/brackets.html), for example: `\stitle="(.*)?"\s*(/>*)`    .

3. In the replace field, [backreference](https://www.regular-expressions.info/backref.html) such groups by numbers starting with 1, for example:

```XML
$2<title>$1</title>
```

.

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

![Replace with regex result](https://resources.jetbrains.com.cn/help/img/idea/2026.2/replace_with_regex.png)

## Switch the character case

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

Procedure:

1. Open the search and replace pane `Ctrl+R` (Windows), `⌘ R` (macOS), `⌘ R` (IntelliJ IDEA Classic (macOS)), `⌘ R` (macOS System Shortcuts), `Ctrl+R` (XWin), `Ctrl+R` (GNOME), `Ctrl+R` (KDE), `Alt+Shift+5` (Emacs), `Ctrl+H` (Sublime Text), `⌘ ⌥ F` (Sublime Text (macOS)), `Ctrl+H` (NetBeans), `Ctrl+H` (Visual Studio), `⌃ H` (Visual Studio (macOS)), `Ctrl+F` (Eclipse), `Ctrl+R` (Eclipse (macOS)). Make sure that ![the Regex icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.inline.regex.svg) 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](https://resources.jetbrains.com.cn/help/img/idea/2026.2/changing_character_with_regex.png)

For more information, refer to the [RegEx syntax](regular-expressions.html) reference table.

## See also

### Getting Started

[Editor basics](using-code-editor.html)

### Reference

[Regular expression syntax reference](regular-expressions.html)

### Procedures

[Search for a target within a file](finding-and-replacing-text-in-file.html)

