Skip to content

Commit e361d7e

Browse files
authored
Adding examples, docs, fixing small issues (#1)
1 parent 61a2dd4 commit e361d7e

File tree

9 files changed

+208
-33
lines changed

9 files changed

+208
-33
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ if (regex.IsMatch("\nabc"))
105105
// abc found!
106106
```
107107

108+
## Validated regular expressions
109+
110+
You'll find in the tests some regular expressions used. Those can be useful:
111+
112+
- email addresses: `([\w\d_.\-]+)@([\d\w\.\-]+)\.([\w\.]{2,5})`
113+
- http(s) URL: `(https?:\/\/)([\da-z-._]+)/?([\/\da-z.-]*)` (limitation: URL has to finish with a / to be properly extracted, this is a bug into our engine, it works perfectly with the expression between ^ and $)
114+
- MD5: `[a-f0-9]{32}`
115+
- SHA256: `[A-Fa-f0-9]{64}`
116+
- Simple XML tag: `<tag>[^<]*</tag>`
117+
- GUID: `[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?`
118+
- Date time like `2021-04-10 18:08:42`: `(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})`
119+
120+
## Known limitations
121+
122+
This parser is a simple one, some of those elements are not supported:
123+
124+
- Expressions like `(?<word>\w+)` will not work. While groups are supported, the `?` in front of a named group or element is not supported.
125+
- For some characters, when using the escaped version like `\.` you may encounter issues, just use `.` instead.
126+
- Sometimes the order of the characters may have an impact. If you are in this case, try to change the order in a character class like `[a-z-._]`
127+
108128
## Feedback and documentation
109129

110130
For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
using nanoFramework.TestFramework;
2+
using System;
3+
using System.Diagnostics;
4+
using System.Text.RegularExpressions;
5+
6+
namespace NFUnitTestRegex
7+
{
8+
[TestClass]
9+
class ComplexExpressionsTests
10+
{
11+
[TestMethod]
12+
public void SingleEmailAddress()
13+
{
14+
Regex rx = new Regex(@"^([\w\d_.\-]+)@([\d\w\.\-]+)\.([\w\.]{2,5})$",
15+
RegexOptions.Compiled | RegexOptions.IgnoreCase);
16+
17+
// Define a test string.
18+
string[] emails = new string[] { "[email protected]", "[email protected]", "[email protected]", "[email protected]" };
19+
foreach (var text in emails)
20+
{
21+
var matches = rx.Matches(text);
22+
foreach (Match match in matches)
23+
{
24+
Debug.WriteLine($"Email found: {match}");
25+
Assert.Equal(text, match.ToString());
26+
}
27+
28+
Assert.Equal(1, matches.Count);
29+
}
30+
}
31+
32+
[TestMethod]
33+
public void ExtractEmailAddress()
34+
{
35+
Regex rx = new Regex(@"([\w\d_.\-]+)@([\d\w\.\-]+)\.([\w\.]{2,5})",
36+
RegexOptions.Compiled | RegexOptions.IgnoreCase);
37+
38+
string text = "this is an email address: [email protected]!\r\n and [email protected] [email protected] [email protected]";
39+
40+
var matches = rx.Matches(text);
41+
foreach (Match match in matches)
42+
{
43+
Debug.WriteLine($"Email found: {match}");
44+
}
45+
46+
Assert.Equal(4, matches.Count);
47+
}
48+
49+
[TestMethod]
50+
public void SingleHttpsAddress()
51+
{
52+
Regex rx = new Regex(@"^(https?:\/\/)([\da-z-._]+)\/?([\/\da-z.-]*)$",
53+
RegexOptions.Compiled | RegexOptions.IgnoreCase);
54+
55+
// Define a test string.
56+
string[] urls = new string[] { "https://github.com/nanoFramework", "https://www1.something-123.com", "http://something/sub", "HTTPS://WWW.flkjlkf/ozrhzor/slkhdflgkh" };
57+
foreach (var text in urls)
58+
{
59+
var matches = rx.Matches(text);
60+
foreach (Match match in matches)
61+
{
62+
Debug.WriteLine($"URL found: {match}");
63+
Assert.Equal(text, match.ToString());
64+
}
65+
66+
Assert.Equal(1, matches.Count);
67+
}
68+
}
69+
70+
[TestMethod]
71+
public void ExtractHttpsAddress()
72+
{
73+
Regex rx = new Regex(@"(https?:\/\/)([\da-z-._]+)/?([\/\da-z.-]*)",
74+
RegexOptions.Compiled | RegexOptions.IgnoreCase);
75+
76+
// Define a test string.
77+
string text = "this is an email address: [email protected]!\r\n and url https://github.com/nanoFramework and there is one more https://www1.something-123.com/ and yet http another http://something/sub HTTPS://WWW.flkjlkf/ozrhzor/slkhdflgkh";
78+
string[] emails = new string[] { "https://github.com/nanoFramework", "https://www1.something-123.com/", "http://something/sub", "HTTPS://WWW.flkjlkf/ozrhzor/slkhdflgkh" };
79+
80+
var matches = rx.Matches(text);
81+
int idx = 0;
82+
foreach (Match match in matches)
83+
{
84+
Debug.WriteLine($"URL found: {match}");
85+
Assert.Equal(emails[idx++], match.ToString());
86+
}
87+
88+
Assert.Equal(4, matches.Count);
89+
}
90+
91+
[TestMethod]
92+
public void MD5Test()
93+
{
94+
string pattern = @"[a-f0-9]{32}";
95+
string validMD5 = "36e8b0061e35a148375d0595492de11f";
96+
string text = $"This is a valid MD5 hash betwwen a Z and Q: Z{validMD5}Q";
97+
Match match = Regex.Match(text, pattern);
98+
Assert.True(match.Success);
99+
Debug.WriteLine($"MD5 Found: {match}");
100+
Assert.Equal(validMD5, match.ToString());
101+
}
102+
103+
[TestMethod]
104+
public void SHA256Test()
105+
{
106+
string pattern = @"[A-Fa-f0-9]{64}";
107+
string Sha256 = "196A8e96Eb8894811f22e7c696BB8D4BB2B57c1E0da3dA69cDC10Bc5899BaB73";
108+
string text = $"A valid MD5 hash will be extracted: Z{Sha256}Q";
109+
Match match = Regex.Match(text, pattern);
110+
Assert.True(match.Success);
111+
Debug.WriteLine($"SHA256 Found: {match}");
112+
Assert.Equal(Sha256, match.ToString());
113+
}
114+
115+
[TestMethod]
116+
public void SimpleXMLTest()
117+
{
118+
string pattern = @"<tag>[^<]*</tag>";
119+
string xml = "<tag>something here</tag>";
120+
string text = $"This is a valid XML tag between a Z and Q: Z{xml}Q";
121+
Match match = Regex.Match(text, pattern);
122+
Assert.True(match.Success);
123+
Debug.WriteLine($"XML tag Found: {match}");
124+
Assert.Equal(xml, match.ToString());
125+
}
126+
127+
[TestMethod]
128+
public void GUIDTest()
129+
{
130+
string pattern = @"[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?";
131+
string guid = "123e4567-e89b-12d3-a456-9AC7CBDCEE52";
132+
string text = $"This is a valid guid between a Z and Q: Z{guid}Q";
133+
Match match = Regex.Match(text, pattern);
134+
Assert.True(match.Success);
135+
Debug.WriteLine($"GUID Found: {match}");
136+
Assert.Equal(guid, match.ToString());
137+
138+
guid = "{123e4567-e89b-12d3-a456-9AC7CBDCEE52}";
139+
text = $"This is a valid guid tag: Z{guid}Q";
140+
match = Regex.Match(text, pattern);
141+
Assert.True(match.Success);
142+
Debug.WriteLine($"GUID Found: {match}");
143+
Assert.Equal(guid, match.ToString());
144+
}
145+
146+
[TestMethod]
147+
public void DateTimeTest()
148+
{
149+
string pattern = @"(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})";
150+
string datetime = "2021-04-10 18:08:42";
151+
string text = $"A valid date time will be extracted: bla{datetime}234";
152+
Match match = Regex.Match(text, pattern);
153+
Assert.True(match.Success);
154+
Debug.WriteLine($"DateTime Found: {match}");
155+
Assert.Equal(datetime, match.ToString());
156+
}
157+
}
158+
}

Tests/NFUnitTestRegex/MatchTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public void RegExpTest_7_Match_Test_01()
7070
[TestMethod]
7171
public void RegExpTest_8_MatchCollection_Test_1()
7272
{
73-
// TODO fix this
74-
// seems that it can't handle named groups
73+
// This is an unsupported scenario in this parser.
74+
// ?<word> is not supported
7575

7676
//// http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchcollection.aspx
7777
//// The example produces the following output to the console:

Tests/NFUnitTestRegex/NFUnitTestRegex.nfproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<RunSettingsFilePath>$(MSBuildProjectDirectory)\nano.runsettings</RunSettingsFilePath>
2828
</PropertyGroup>
2929
<ItemGroup>
30+
<Compile Include="ComplexExpressionsTests.cs" />
3031
<Compile Include="GroupTests.cs" />
3132
<Compile Include="MatchTests.cs" />
3233
<Compile Include="RegexpOptionTests.cs" />
@@ -44,13 +45,13 @@
4445
<Private>True</Private>
4546
<SpecificVersion>True</SpecificVersion>
4647
</Reference>
47-
<Reference Include="nanoFramework.TestFramework, Version=1.0.98.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
48-
<HintPath>..\..\packages\nanoFramework.TestFramework.1.0.98\lib\nanoFramework.TestFramework.dll</HintPath>
48+
<Reference Include="nanoFramework.TestFramework, Version=1.0.101.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
49+
<HintPath>..\..\packages\nanoFramework.TestFramework.1.0.101\lib\nanoFramework.TestFramework.dll</HintPath>
4950
<Private>True</Private>
5051
<SpecificVersion>True</SpecificVersion>
5152
</Reference>
5253
<Reference Include="nanoFramework.UnitTestLauncher, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
53-
<HintPath>..\..\packages\nanoFramework.TestFramework.1.0.98\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
54+
<HintPath>..\..\packages\nanoFramework.TestFramework.1.0.101\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
5455
<Private>True</Private>
5556
<SpecificVersion>True</SpecificVersion>
5657
</Reference>
@@ -61,7 +62,7 @@
6162
</ItemGroup>
6263
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
6364
<!-- MANUAL UPDATE HERE -->
64-
<Import Project="..\..\packages\nanoFramework.TestFramework.1.0.98\build\nanoFramework.TestFramework.targets" Condition="Exists('..\..\packages\nanoFramework.TestFramework.1.0.98\build\nanoFramework.TestFramework.targets')" />
65+
<Import Project="..\..\packages\nanoFramework.TestFramework.1.0.101\build\nanoFramework.TestFramework.targets" Condition="Exists('..\..\packages\nanoFramework.TestFramework.1.0.101\build\nanoFramework.TestFramework.targets')" />
6566
<ProjectExtensions>
6667
<ProjectCapabilities>
6768
<ProjectConfigurationsDeclaredAsItems />
@@ -71,6 +72,6 @@
7172
<PropertyGroup>
7273
<WarningText>Update the Import path in nfproj to the correct nanoFramework.TestFramework NuGet package folder.</WarningText>
7374
</PropertyGroup>
74-
<Warning Condition="!Exists('..\..\packages\nanoFramework.TestFramework.1.0.98\build\nanoFramework.TestFramework.targets')" Text="'$(WarningText)'" />
75+
<Warning Condition="!Exists('..\..\packages\nanoFramework.TestFramework.1.0.101\build\nanoFramework.TestFramework.targets')" Text="'$(WarningText)'" />
7576
</Target>
7677
</Project>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="nanoFramework.CoreLibrary" version="1.10.3-preview.7" targetFramework="netnanoframework10" />
4-
<package id="nanoFramework.TestFramework" version="1.0.98" targetFramework="netnanoframework10" developmentDependency="true" />
4+
<package id="nanoFramework.TestFramework" version="1.0.101" targetFramework="netnanoframework10" developmentDependency="true" />
55
</packages>

nanoFramework.System.Text.RegularExpressions/Classes/Regex.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,10 +1631,8 @@ public string Replace(string substituteIn, string substitution)
16311631
/// </summary>
16321632
/// <param name="substituteIn">String to substitute within</param>
16331633
/// <param name="substitution">String to substitute for matches of this regular expression</param>
1634-
/// <param name="flags"> One or more bitwise flags from ReplaceOptions. If the ReplaceFirstOnly
1635-
/// flag bit is set, only the first occurrence of this regular expression is replaced.
1636-
/// If the bit is not set (ReplaceAll), all occurrences of this pattern will be
1637-
/// replaced. If the flag ReplaceBackrefrences is set, all backreferences will be processed.</param>
1634+
/// <param name="maxOccurances">Maximum number of occurrences</param>
1635+
/// <param name="start">Start index</param>
16381636
/// <returns>The string substituteIn with zero or more occurrences of the current
16391637
/// regular expression replaced with the substitution String (if this regular
16401638
/// expression object doesn't match at any position, the original String is returned
@@ -1761,6 +1759,8 @@ public string[] GetMatches(string search)
17611759
/// aaaab], the array of Strings returned by grep would be [aab, aaaab].
17621760
/// </summary>
17631761
/// <param name="search">Array of string to search</param>
1762+
/// <param name="start">Start index</param>
1763+
/// <param name="length">The length</param>
17641764
/// <returns>Array of Strings whose value matches this regular expression</returns>
17651765
public string[] GetMatches(string[] search, int start, int length)
17661766
{
@@ -1865,9 +1865,10 @@ internal Match Scan(Regex regex, string text, int textbeg, int textend, int text
18651865
/// <summary>
18661866
/// Adds a match to a Match
18671867
/// </summary>
1868-
/// <param name="capnum"></param>
1869-
/// <param name="start"></param>
1870-
/// <param name="end"></param>
1868+
/// <param name="match">The match</param>
1869+
/// <param name="capnum">The capture number</param>
1870+
/// <param name="start">The start index</param>
1871+
/// <param name="end">The end index</param>
18711872
internal void Capture(Match match, int capnum, int start, int end)
18721873
{
18731874
if (end < start)

nanoFramework.System.Text.RegularExpressions/Collections/GroupCollection.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
using System.Collections;
88

99
namespace System.Text.RegularExpressions
10-
{
10+
{
11+
/// <summary>
12+
/// Group Collection class
13+
/// </summary>
1114
[Serializable]
1215
public class GroupCollection : ICollection
1316
{

nanoFramework.System.Text.RegularExpressions/nanoFramework.System.text.RegularExpressions.nfproj

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,10 @@
5757
<NFMDP_PE_LoadHints Include="..\packages\nanoFramework.CoreLibrary.1.10.3-preview.7\lib\mscorlib.dll">
5858
<InProject>false</InProject>
5959
</NFMDP_PE_LoadHints>
60-
<NFMDP_PE_LoadHints Include="..\packages\nanoFramework.System.Collections.1.2.0-preview.36\lib\nanoFramework.System.Collections.dll">
60+
<NFMDP_PE_LoadHints Include="..\packages\nanoFramework.System.Collections.1.2.0-preview.39\lib\nanoFramework.System.Collections.dll">
6161
<InProject>false</InProject>
6262
</NFMDP_PE_LoadHints>
63-
<NFMDP_PE_LoadHints Include="..\packages\nanoFramework.System.Text.1.1.1-preview.35\lib\nanoFramework.System.Text.dll">
64-
<InProject>false</InProject>
65-
</NFMDP_PE_LoadHints>
66-
<NFMDP_PE_LoadHints Include="..\packages\nanoFramework.System.IO.FileSystem.1.0.0-preview.23\lib\System.IO.FileSystem.dll">
63+
<NFMDP_PE_LoadHints Include="..\packages\nanoFramework.System.Text.1.1.1-preview.40\lib\nanoFramework.System.Text.dll">
6764
<InProject>false</InProject>
6865
</NFMDP_PE_LoadHints>
6966
</ItemGroup>
@@ -77,17 +74,12 @@
7774
<SpecificVersion>True</SpecificVersion>
7875
</Reference>
7976
<Reference Include="nanoFramework.System.Collections, Version=1.2.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
80-
<HintPath>..\packages\nanoFramework.System.Collections.1.2.0-preview.36\lib\nanoFramework.System.Collections.dll</HintPath>
77+
<HintPath>..\packages\nanoFramework.System.Collections.1.2.0-preview.39\lib\nanoFramework.System.Collections.dll</HintPath>
8178
<Private>True</Private>
8279
<SpecificVersion>True</SpecificVersion>
8380
</Reference>
8481
<Reference Include="nanoFramework.System.Text, Version=1.1.1.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
85-
<HintPath>..\packages\nanoFramework.System.Text.1.1.1-preview.35\lib\nanoFramework.System.Text.dll</HintPath>
86-
<Private>True</Private>
87-
<SpecificVersion>True</SpecificVersion>
88-
</Reference>
89-
<Reference Include="System.IO.FileSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
90-
<HintPath>..\packages\nanoFramework.System.IO.FileSystem.1.0.0-preview.30\lib\System.IO.FileSystem.dll</HintPath>
82+
<HintPath>..\packages\nanoFramework.System.Text.1.1.1-preview.40\lib\nanoFramework.System.Text.dll</HintPath>
9183
<Private>True</Private>
9284
<SpecificVersion>True</SpecificVersion>
9385
</Reference>
@@ -98,10 +90,11 @@
9890
<ProjectConfigurationsDeclaredAsItems />
9991
</ProjectCapabilities>
10092
</ProjectExtensions>
93+
<Import Project="..\packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets')" />
10194
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
10295
<PropertyGroup>
10396
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
10497
</PropertyGroup>
105-
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.3.37\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.3.37\build\Nerdbank.GitVersioning.targets'))" />
98+
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets'))" />
10699
</Target>
107100
</Project>
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="nanoFramework.CoreLibrary" version="1.10.3-preview.7" targetFramework="netnanoframework10" />
4-
<package id="nanoFramework.System.Collections" version="1.2.0-preview.36" targetFramework="netnanoframework10" />
5-
<package id="nanoFramework.System.IO.FileSystem" version="1.0.0-preview.30" targetFramework="netnanoframework10" />
6-
<package id="nanoFramework.System.Text" version="1.1.1-preview.35" targetFramework="netnanoframework10" />
7-
<package id="Nerdbank.GitVersioning" version="3.3.37" developmentDependency="true" targetFramework="netnanoframework10" />
4+
<package id="nanoFramework.System.Collections" version="1.2.0-preview.39" targetFramework="netnanoframework10" />
5+
<package id="nanoFramework.System.Text" version="1.1.1-preview.40" targetFramework="netnanoframework10" />
6+
<package id="Nerdbank.GitVersioning" version="3.4.194" developmentDependency="true" targetFramework="netnanoframework10" />
87
</packages>

0 commit comments

Comments
 (0)