2023-06-05 18:08:52 +02:00
|
|
|
include::../why-dotnet.adoc[]
|
2023-05-03 11:06:20 +02:00
|
|
|
|
2023-06-05 18:08:52 +02:00
|
|
|
== How to fix it
|
2023-03-22 15:25:58 +01:00
|
|
|
|
2023-06-05 18:08:52 +02:00
|
|
|
=== Code examples
|
2023-03-22 15:25:58 +01:00
|
|
|
|
2023-06-05 18:08:52 +02:00
|
|
|
==== Noncompliant code example
|
2023-03-22 15:25:58 +01:00
|
|
|
|
2023-06-05 18:08:52 +02:00
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
2023-03-22 15:25:58 +01:00
|
|
|
----
|
2023-06-05 18:08:52 +02:00
|
|
|
void Regexes(string input)
|
2023-03-22 15:25:58 +01:00
|
|
|
{
|
2023-06-05 18:08:52 +02:00
|
|
|
var regex = new Regex("[A"); // Noncompliant
|
|
|
|
var match = Regex.Match(input, "[A"); // Noncompliant
|
2023-03-22 15:25:58 +01:00
|
|
|
var negativeLookahead = new Regex("a(?!b)", RegexOptions.NonBacktracking); // Noncompliant
|
|
|
|
var negativeLookbehind = new Regex("(?<!a)b", RegexOptions.NonBacktracking); // Noncompliant
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2023-06-05 18:08:52 +02:00
|
|
|
==== Compliant solution
|
2023-03-22 15:25:58 +01:00
|
|
|
|
2023-06-05 18:08:52 +02:00
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
2023-03-22 15:25:58 +01:00
|
|
|
----
|
2023-06-05 18:08:52 +02:00
|
|
|
void Regexes(string input)
|
2023-03-22 15:25:58 +01:00
|
|
|
{
|
|
|
|
var regex = new Regex("[A-Z]");
|
|
|
|
var match = Regex.Match(input, "[A-Z]");
|
|
|
|
var negativeLookahead = new Regex("a(?!b)");
|
|
|
|
var negativeLookbehind = new Regex("(?<!a)b");
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2023-06-05 18:08:52 +02:00
|
|
|
include::../resources-dotnet.adoc[]
|
2023-03-22 15:25:58 +01:00
|
|
|
include::../rspecator.adoc[]
|