|
| 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 | + |
| 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 | + |
| 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 | +} |
0 commit comments