2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-03-22 15:25:58 +01:00
include::../description.adoc[]
Negative lookahead and negative lookbehind groups cannot be combined with `RegexOptions.NonBacktracking`. Such combination would throw an exception during runtime.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2023-03-22 15:25:58 +01:00
[source,vbnet]
----
Public Sub DoSomething(Input As String)
Dim Rx As New Regex("[A") ' Noncompliant
Dim Match = Regex.Match(Input, "[A") ' Noncompliant
Dim Matches = Regex.Matches(Input, "[A") ' Noncompliant
Dim Replace = Regex.Replace(Input, "[A", "replacement") ' Noncompliant
Dim Split = Regex.Split(Input, "[A") ' Noncompliant
If (Regex.IsMatch(Input, "[A")) Then ' Noncompliant
End If
Dim NegativeLookahead As New Regex("a(?!b)", RegexOptions.NonBacktracking) ' Noncompliant
Dim NegativeLookbehind As New Regex("(?<!a)b", RegexOptions.NonBacktracking) ' Noncompliant
End Sub
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2023-03-22 15:25:58 +01:00
[source,vbnet]
----
Public Sub DoSomething(Input As String)
Dim Rx As New Regex("[A-Z]")
Dim Match = Regex.Match(Input, "[A-Z]")
Dim Matches = Regex.Matches(Input, "[A-Z]")
Dim Replace = Regex.Replace(Input, "[A-Z]", "replacement")
Dim Split = Regex.Split(Input, "[A-Z]")
If (Regex.IsMatch(Input, "[A-Z]")) Then
End If
Dim NegativeLookahead As New Regex("a(?!b)")
Dim NegativeLookbehind As New Regex("(?<!a)b")
End Sub
----
include::../rspecator.adoc[]