PhpStorm 2023.3 Help

Code Inspections in RegExp

This topic lists all PhpStorm code inspections available in RegExp.

You can toggle specific inspections or change their severity level on the Editor | Inspections page of the IDE settingsĀ  Ctrl+Alt+S.

Inspection

Description

Default Severity

Anonymous capturing group or numeric back reference

Reports anonymous capturing groups and numeric back references in a RegExp. These are only reported when the RegExp dialect supports named group and named group references. Named groups and named back references improve code readability and are recommended to use instead. When a capture is not needed, matching can be more performant and use less memory by using a non-capturing group, i.e. (?:xxx) instead of (xxx).

Example:

(\d\d\d\d)\1

A better regex pattern could look like this:

(?<quad>\d\d\d\d)\k<quad>

New in 2017.2

Disabled

Begin or end anchor in unexpected position

Reports ^ or \A anchors not at the beginning of the pattern and $, \Z or \z anchors not at the end of the pattern. In the wrong position these RegExp anchors prevent the pattern from matching anything. In case of the ^ and $ anchors, most likely the literal character was meant and the escape forgotten.

Example:

(Price $10)

New in 2018.1

Warning Warning

Consecutive spaces

Reports multiple consecutive spaces in a RegExp. Because spaces are not visible by default, it can be hard to see how many spaces are required. The RegExp can be made more clear by replacing the consecutive spaces with a single space and a counted quantifier.

Example:

( )

After the quick-fix is applied:

( {5})

New in 2017.1

Warning Warning

Custom RegExp inspection

Custom Regex Inspection

Warning Warning

Duplicate branch in alternation

Reports duplicate branches in a RegExp alternation. Duplicate branches slow down matching and obscure the intent of the expression.

Example:

(alpha|bravo|charlie|alpha)

After the quick-fix is applied:

(alpha|bravo|charlie)

New in 2017.1

Warning Warning

Duplicate character in character class

Reports duplicate characters inside a RegExp character class. Duplicate characters are unnecessary and can be removed without changing the semantics of the regex.

Example:

[aabc]

After the quick-fix is applied:

[abc]

Warning Warning

Empty branch in alternation

Reports empty branches in a RegExp alternation. An empty branch will only match the empty string, and in most cases that is not what is desired. This inspection will not report a single empty branch at the start or the end of an alternation.

Example:

(alpha||bravo)

After the quick-fix is applied:

(alpha|bravo)

New in 2017.2

Warning Warning

Escaped meta character

Reports escaped meta characters. Some RegExp coding styles specify that meta characters should be placed inside a character class, to make the regular expression easier to understand. This inspection does not warn about the meta character [, ] and ^, because those would need additional escaping inside a character class.

Example:

\d+\.\d+

After the quick-fix is applied:

\d+[.]\d+

New in 2017.1

Info No highlighting, only fix

Octal escape

Reports octal escapes, which are easily confused with back references. Use hexadecimal escapes to avoid confusion.

Example:

\07

After the quick-fix is applied:

\x07

New in 2017.1

Info No highlighting, only fix

Redundant '\d', '[:digit:]', or '\D' class elements

Reports redundant \d or [:digit:] that are used in one class with \w or [:word:] (\D with \W) and can be removed.

Example:

[\w\d]

After the quick-fix is applied:

[\w]

New in 2022.2

Weak Warning Weak warning

Redundant character escape

Reports redundant character escape sequences that can be replaced with unescaped characters preserving the meaning. Many escape sequences that are necessary outside of a character class are redundant inside square brackets [] of a character class.

Although unescaped opening curly braces { outside of character classes are allowed in some dialects (JavaScript, Python, and so on), it can cause confusion and make the pattern less portable, because there are dialects that require escaping curly braces as characters. For this reason the inspection does not report escaped opening curly braces.

Example:

\-\;[\.]

After the quick-fix is applied:

-;[.]

The Ignore escaped closing brackets '}' and ']' option specifies whether to report \} and \] outside of a character class when they are allowed to be unescaped by the RegExp dialect.

New in 2017.3

Warning Warning

Redundant nested character class

Reports unnecessary nested character classes.

Example:

[a-c[x-z]]

After the quick-fix is applied:

[a-cx-z]

New in 2020.2

Warning Warning

Regular expression can be simplified

Reports regular expressions that can be simplified.

Example:

[a] xx* [ah-hz]

After the quick-fix is applied:

a x+ [ahz]

New in 2022.1

Weak Warning Weak warning

Single character alternation

Reports single char alternation in a RegExp. It is simpler to use a character class instead. This may also provide better matching performance.

Example:

a|b|c|d

After the quick-fix is applied:

[abcd]

New in 2017.1

Warning Warning

Suspicious back reference

Reports back references that will not be resolvable at runtime. This means that the back reference can never match anything. A back reference will not be resolvable when the group is defined after the back reference, or if the group is defined in a different branch of an alternation.

Example of a group defined after its back reference:

\1(abc)

Example of a group and a back reference in different branches:

a(b)c|(xy)\1z

New in 2022.1

Warning Warning

Unnecessary non-capturing group

Reports unnecessary non-capturing groups, which have no influence on the match result.

Example:

Everybody be cool, (?:this) is a robbery!

After the quick-fix is applied:

Everybody be cool, this is a robbery!

New in 2021.1

Warning Warning

Last modified: 25 March 2024