S5856: Add C# and VB.NET (#1528)

This commit is contained in:
Pavel Mikula 2023-03-22 15:25:58 +01:00 committed by GitHub
parent 51b0652467
commit a8c0d761a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 101 additions and 23 deletions

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1,43 @@
include::../description.adoc[]
Negative lookahead and negative lookbehind groups cannot be combined with `RegexOptions.NonBacktracking`. Such combination would throw an exception during runtime.
== Noncompliant Code Example
[source,csharp]
----
public void DoSomething(string input)
{
var regex = new Regex("[A"); // Noncompliant
var match = Regex.Match(input, "[A"); // Noncompliant
var matches = Regex.Matches(input, "[A"); // Noncompliant
var replace = Regex.Replace(input, "[A", "replacement"); // Noncompliant
var split = Regex.Split(input, "[A"); // Noncompliant
if(Regex.IsMatch(input, "[A")) // Noncompliant
{
}
var negativeLookahead = new Regex("a(?!b)", RegexOptions.NonBacktracking); // Noncompliant
var negativeLookbehind = new Regex("(?<!a)b", RegexOptions.NonBacktracking); // Noncompliant
}
----
== Compliant Solution
[source,csharp]
----
public void DoSomething(string input)
{
var regex = new Regex("[A-Z]");
var match = Regex.Match(input, "[A-Z]");
var matches = Regex.Matches(input, "[A-Z]");
var replace = Regex.Replace(input, "[A-Z]", "replacement");
var split = Regex.Split(input, "[A-Z]");
if(Regex.IsMatch(input, "[A-Z]"))
{
}
var negativeLookahead = new Regex("a(?!b)");
var negativeLookbehind = new Regex("(?<!a)b");
}
----
include::../rspecator.adoc[]

View File

@ -25,14 +25,4 @@ str.replace("([", "{");
str.matches("(\\w+)-(\\d+)");
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
endif::env-github,rspecator-view[]
include::../rspecator.adoc[]

View File

@ -19,3 +19,4 @@ str.match("\\(\\[");
str.replace("([", "{");
----
include::../rspecator.adoc[]

View File

@ -25,14 +25,4 @@ Regex("([", RegexOption.LITERAL)
"([".toRegex(RegexOption.LITERAL)
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
endif::env-github,rspecator-view[]
include::../rspecator.adoc[]

View File

@ -1,4 +1,4 @@
=== Message
Fix this syntax error inside this regex
Fix the syntax error inside this regex

View File

@ -22,3 +22,4 @@ str_replace("([", "{", $input);
preg_replace("/(\\w+)-(\\d+)/", "1234", $input);
----
include::../rspecator.adoc[]

View File

@ -22,3 +22,4 @@ input.replace("([", "{")
re.compile(r"(\w+)-(\d+)")
----
include::../rspecator.adoc[]

View File

@ -0,0 +1,11 @@
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1,39 @@
include::../description.adoc[]
Negative lookahead and negative lookbehind groups cannot be combined with `RegexOptions.NonBacktracking`. Such combination would throw an exception during runtime.
== Noncompliant Code Example
[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
----
== Compliant Solution
[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[]