## Why is this an issue?

Regular expressions have their own syntax that is understood by regular expression engines. Those engines will throw an exception at runtime if they are given a regular expression that does not conform to that syntax.

To avoid syntax errors, special characters should be escaped with backslashes when they are intended to be matched literally and references to capturing groups should use the correctly spelled name or number of the group.

Negative lookaround groups cannot be combined with RegexOptions.NonBacktracking. Such combination would throw an exception during runtime.

## How to fix it

### Code examples

#### Noncompliant code example

    Sub Regexes(Input As String)
        Dim Rx As New Regex("[A")                                                       ' Noncompliant: unmatched "["
        Dim Match = Regex.Match(Input, "[A")                                            ' Noncompliant
        Dim NegativeLookahead As New Regex("a(?!b)", RegexOptions.NonBacktracking)      ' Noncompliant: negative lookahead without backtracking
        Dim NegativeLookbehind As New Regex("(?<!a)b", RegexOptions.NonBacktracking)    ' Noncompliant: negative lookbehind without backtracking
    End Sub

#### Compliant solution

    Sub Regexes(Input As String)
        Dim Rx As New Regex("[A-Z]")
        Dim Match = Regex.Match(Input, "[A-Z]")
        Dim NegativeLookahead As New Regex("a(?!b)")
        Dim NegativeLookbehind As New Regex("(?<!a)b")
    End Sub

## Resources

### Documentation

- .NET Regular expressions
- Lookarounds at a glance
- Backtracking in Regular Expressions
- Character Escapes in Regular Expressions