Skip to content

Commit 93d66d5

Browse files
authored
Update right-to-left regex search section (dotnet#29708)
1 parent ce32858 commit 93d66d5

35 files changed

+473
-441
lines changed

docs/standard/base-types/regular-expression-options.md

+41-28
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFrameworks>net6.0</TargetFrameworks>
6+
</PropertyGroup>
7+
</Project>

samples/snippets/csharp/VS_Snippets_CLR/conceptual.regex.language.options/cs/case2.cs

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
using System;
33
using System.Text.RegularExpressions;
44

5-
public class Example
5+
public class CaseExample
66
{
7-
public static void Main()
8-
{
9-
string pattern = @"\b(?i:t)he\w*\b";
10-
string input = "The man then told them about that event.";
11-
foreach (Match match in Regex.Matches(input, pattern))
12-
Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index);
7+
public static void Main()
8+
{
9+
string pattern = @"\b(?i:t)he\w*\b";
10+
string input = "The man then told them about that event.";
11+
foreach (Match match in Regex.Matches(input, pattern))
12+
Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index);
1313

14-
Console.WriteLine();
15-
pattern = @"(?i)\bthe\w*\b";
16-
foreach (Match match in Regex.Matches(input, pattern,
17-
RegexOptions.IgnoreCase))
18-
Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index);
19-
}
14+
Console.WriteLine();
15+
pattern = @"(?i)\bthe\w*\b";
16+
foreach (Match match in Regex.Matches(input, pattern,
17+
RegexOptions.IgnoreCase))
18+
Console.WriteLine("Found {0} at index {1}.", match.Value, match.Index);
19+
}
2020
}
2121
// The example displays the following output:
2222
// Found The at index 0.

samples/snippets/csharp/VS_Snippets_CLR/conceptual.regex.language.options/cs/culture1.cs

+53-53
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,58 @@
33
using System.Text.RegularExpressions;
44
using System.Threading;
55

6-
public class Example
6+
public class CultureExample
77
{
8-
public static void Main()
9-
{
10-
ShowIllegalAccess();
11-
Console.WriteLine("-----");
12-
ShowNoAccess();
13-
}
14-
15-
private static void ShowIllegalAccess()
16-
{
17-
// <Snippet14>
18-
CultureInfo defaultCulture = Thread.CurrentThread.CurrentCulture;
19-
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
20-
21-
string input = "file://c:/Documents.MyReport.doc";
22-
string pattern = "FILE://";
23-
24-
Console.WriteLine("Culture-sensitive matching ({0} culture)...",
25-
Thread.CurrentThread.CurrentCulture.Name);
26-
if (Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase))
27-
Console.WriteLine("URLs that access files are not allowed.");
28-
else
29-
Console.WriteLine("Access to {0} is allowed.", input);
30-
31-
Thread.CurrentThread.CurrentCulture = defaultCulture;
32-
// The example displays the following output:
33-
// Culture-sensitive matching (tr-TR culture)...
34-
// Access to file://c:/Documents.MyReport.doc is allowed.
35-
// </Snippet14>
36-
}
37-
38-
private static void ShowNoAccess()
39-
{
40-
// <Snippet15>
41-
CultureInfo defaultCulture = Thread.CurrentThread.CurrentCulture;
42-
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
43-
44-
string input = "file://c:/Documents.MyReport.doc";
45-
string pattern = "FILE://";
46-
47-
Console.WriteLine("Culture-insensitive matching...");
48-
if (Regex.IsMatch(input, pattern,
49-
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
50-
Console.WriteLine("URLs that access files are not allowed.");
51-
else
52-
Console.WriteLine("Access to {0} is allowed.", input);
53-
54-
Thread.CurrentThread.CurrentCulture = defaultCulture;
55-
// The example displays the following output:
56-
// Culture-insensitive matching...
57-
// URLs that access files are not allowed.
58-
// </Snippet15>
59-
}
8+
public static void Main()
9+
{
10+
ShowIllegalAccess();
11+
Console.WriteLine("-----");
12+
ShowNoAccess();
13+
}
14+
15+
private static void ShowIllegalAccess()
16+
{
17+
// <Snippet14>
18+
CultureInfo defaultCulture = Thread.CurrentThread.CurrentCulture;
19+
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
20+
21+
string input = "file://c:/Documents.MyReport.doc";
22+
string pattern = "FILE://";
23+
24+
Console.WriteLine("Culture-sensitive matching ({0} culture)...",
25+
Thread.CurrentThread.CurrentCulture.Name);
26+
if (Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase))
27+
Console.WriteLine("URLs that access files are not allowed.");
28+
else
29+
Console.WriteLine("Access to {0} is allowed.", input);
30+
31+
Thread.CurrentThread.CurrentCulture = defaultCulture;
32+
// The example displays the following output:
33+
// Culture-sensitive matching (tr-TR culture)...
34+
// Access to file://c:/Documents.MyReport.doc is allowed.
35+
// </Snippet14>
36+
}
37+
38+
private static void ShowNoAccess()
39+
{
40+
// <Snippet15>
41+
CultureInfo defaultCulture = Thread.CurrentThread.CurrentCulture;
42+
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
43+
44+
string input = "file://c:/Documents.MyReport.doc";
45+
string pattern = "FILE://";
46+
47+
Console.WriteLine("Culture-insensitive matching...");
48+
if (Regex.IsMatch(input, pattern,
49+
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
50+
Console.WriteLine("URLs that access files are not allowed.");
51+
else
52+
Console.WriteLine("Access to {0} is allowed.", input);
53+
54+
Thread.CurrentThread.CurrentCulture = defaultCulture;
55+
// The example displays the following output:
56+
// Culture-insensitive matching...
57+
// URLs that access files are not allowed.
58+
// </Snippet15>
59+
}
6060
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
using System;
22
using System.Text.RegularExpressions;
33

4-
public class Example
4+
public class DetermineOptionsExample
55
{
6-
public static void Main()
7-
{
8-
Regex rgx = new Regex(@"\w*\s", RegexOptions.IgnoreCase);
9-
// <Snippet19>
10-
if ((rgx.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase)
11-
Console.WriteLine("Case-insensitive pattern comparison.");
12-
else
13-
Console.WriteLine("Case-sensitive pattern comparison.");
14-
// </Snippet19>
15-
// <Snippet20>
16-
if (rgx.Options == RegexOptions.None)
17-
Console.WriteLine("No options have been set.");
18-
// </Snippet20>
19-
}
6+
public static void Main()
7+
{
8+
Regex rgx = new Regex(@"\w*\s", RegexOptions.IgnoreCase);
9+
// <Snippet19>
10+
if ((rgx.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase)
11+
Console.WriteLine("Case-insensitive pattern comparison.");
12+
else
13+
Console.WriteLine("Case-sensitive pattern comparison.");
14+
// </Snippet19>
15+
// <Snippet20>
16+
if (rgx.Options == RegexOptions.None)
17+
Console.WriteLine("No options have been set.");
18+
// </Snippet20>
19+
}
2020
}

samples/snippets/csharp/VS_Snippets_CLR/conceptual.regex.language.options/cs/ecmascript1.cs

+20-20
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22
using System;
33
using System.Text.RegularExpressions;
44

5-
public class Example
5+
public class EcmaScriptExample
66
{
7-
public static void Main()
8-
{
9-
string[] values = { "целый мир", "the whole world" };
10-
string pattern = @"\b(\w+\s*)+";
11-
foreach (var value in values)
12-
{
13-
Console.Write("Canonical matching: ");
14-
if (Regex.IsMatch(value, pattern))
15-
Console.WriteLine("'{0}' matches the pattern.", value);
16-
else
17-
Console.WriteLine("{0} does not match the pattern.", value);
7+
public static void Main()
8+
{
9+
string[] values = { "целый мир", "the whole world" };
10+
string pattern = @"\b(\w+\s*)+";
11+
foreach (var value in values)
12+
{
13+
Console.Write("Canonical matching: ");
14+
if (Regex.IsMatch(value, pattern))
15+
Console.WriteLine("'{0}' matches the pattern.", value);
16+
else
17+
Console.WriteLine("{0} does not match the pattern.", value);
1818

19-
Console.Write("ECMAScript matching: ");
20-
if (Regex.IsMatch(value, pattern, RegexOptions.ECMAScript))
21-
Console.WriteLine("'{0}' matches the pattern.", value);
22-
else
23-
Console.WriteLine("{0} does not match the pattern.", value);
24-
Console.WriteLine();
25-
}
26-
}
19+
Console.Write("ECMAScript matching: ");
20+
if (Regex.IsMatch(value, pattern, RegexOptions.ECMAScript))
21+
Console.WriteLine("'{0}' matches the pattern.", value);
22+
else
23+
Console.WriteLine("{0} does not match the pattern.", value);
24+
Console.WriteLine();
25+
}
26+
}
2727
}
2828
// The example displays the following output:
2929
// Canonical matching: 'целый мир' matches the pattern.

samples/snippets/csharp/VS_Snippets_CLR/conceptual.regex.language.options/cs/ecmascript2.cs

+34-34
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,47 @@
22
using System;
33
using System.Text.RegularExpressions;
44

5-
public class Example
5+
public class EcmaScript2Example
66
{
7-
static string pattern;
7+
static string pattern;
88

9-
public static void Main()
10-
{
11-
string input = "aa aaaa aaaaaa ";
12-
pattern = @"((a+)(\1) ?)+";
9+
public static void Main()
10+
{
11+
string input = "aa aaaa aaaaaa ";
12+
pattern = @"((a+)(\1) ?)+";
1313

14-
// Match input using canonical matching.
15-
AnalyzeMatch(Regex.Match(input, pattern));
14+
// Match input using canonical matching.
15+
AnalyzeMatch(Regex.Match(input, pattern));
1616

17-
// Match input using ECMAScript.
18-
AnalyzeMatch(Regex.Match(input, pattern, RegexOptions.ECMAScript));
19-
}
17+
// Match input using ECMAScript.
18+
AnalyzeMatch(Regex.Match(input, pattern, RegexOptions.ECMAScript));
19+
}
2020

21-
private static void AnalyzeMatch(Match m)
22-
{
23-
if (m.Success)
24-
{
25-
Console.WriteLine("'{0}' matches {1} at position {2}.",
26-
pattern, m.Value, m.Index);
27-
int grpCtr = 0;
28-
foreach (Group grp in m.Groups)
29-
{
30-
Console.WriteLine(" {0}: '{1}'", grpCtr, grp.Value);
31-
grpCtr++;
32-
int capCtr = 0;
33-
foreach (Capture cap in grp.Captures)
21+
private static void AnalyzeMatch(Match m)
22+
{
23+
if (m.Success)
24+
{
25+
Console.WriteLine("'{0}' matches {1} at position {2}.",
26+
pattern, m.Value, m.Index);
27+
int grpCtr = 0;
28+
foreach (Group grp in m.Groups)
3429
{
35-
Console.WriteLine(" {0}: '{1}'", capCtr, cap.Value);
36-
capCtr++;
30+
Console.WriteLine(" {0}: '{1}'", grpCtr, grp.Value);
31+
grpCtr++;
32+
int capCtr = 0;
33+
foreach (Capture cap in grp.Captures)
34+
{
35+
Console.WriteLine(" {0}: '{1}'", capCtr, cap.Value);
36+
capCtr++;
37+
}
3738
}
38-
}
39-
}
40-
else
41-
{
42-
Console.WriteLine("No match found.");
43-
}
44-
Console.WriteLine();
45-
}
39+
}
40+
else
41+
{
42+
Console.WriteLine("No match found.");
43+
}
44+
Console.WriteLine();
45+
}
4646
}
4747
// The example displays the following output:
4848
// No match found.

0 commit comments

Comments
 (0)