2 // MonoTests.System.Text.RegularExpressions misc. test cases
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (c) Copyright 2003,2004 Novell, Inc. (http://www.novell.com)
10 using NUnit.Framework;
12 using System.Text.RegularExpressions;
14 namespace MonoTests.System.Text.RegularExpressions
17 public class RegexBugs : Assertion
20 public void SplitGroup () // bug51146
22 string [] splitResult = new Regex ("-").Split ("a-bcd-e-fg");
23 string [] expected = new string [] {"a", "bcd", "e", "fg"};
24 int length = expected.Length;
26 AssertEquals ("#01", length, splitResult.Length);
27 for (i = 0; i < length; i++)
28 AssertEquals ("#02 " + i, expected [i], splitResult [i]);
30 splitResult = new Regex ("(-)").Split ("a-bcd-e-fg");
31 expected = new string [] {"a", "-", "bcd", "-", "e", "-", "fg"};
32 length = expected.Length;
33 AssertEquals ("#03", length, splitResult.Length);
34 for (i = 0; i < length; i++)
35 AssertEquals ("#04 " + i, expected [i], splitResult [i]);
37 splitResult = new Regex ("(-)b(c)").Split ("a-bcd-e-fg");
38 expected = new string [] {"a", "-", "c", "d-e-fg" };
39 length = expected.Length;
40 AssertEquals ("#04", length, splitResult.Length);
41 for (i = 0; i < length; i++)
42 AssertEquals ("#05 " + i, expected [i], splitResult [i]);
44 splitResult = new Regex ("-").Split ("a-bcd-e-fg-");
45 expected = new string [] {"a", "bcd", "e", "fg", ""};
46 length = expected.Length;
47 AssertEquals ("#06", length, splitResult.Length);
48 for (i = 0; i < length; i++)
49 AssertEquals ("#07 " + i, expected [i], splitResult [i]);
53 public void MathEmptyGroup () // bug 42529
55 string str = "Match something from here.";
57 AssertEquals ("MEG #01", false, Regex.IsMatch(str, @"(something|dog)$"));
58 AssertEquals ("MEG #02", true, Regex.IsMatch(str, @"(|something|dog)$"));
59 AssertEquals ("MEG #03", true, Regex.IsMatch(str, @"(something||dog)$"));
60 AssertEquals ("MEG #04", true, Regex.IsMatch(str, @"(something|dog|)$"));
62 AssertEquals ("MEG #05", true, Regex.IsMatch(str, @"(something|dog)*"));
63 AssertEquals ("MEG #06", true, Regex.IsMatch(str, @"(|something|dog)*"));
64 AssertEquals ("MEG #07", true, Regex.IsMatch(str, @"(something||dog)*"));
65 AssertEquals ("MEG #08", true, Regex.IsMatch(str, @"(something|dog|)*"));
67 AssertEquals ("MEG #09", true, Regex.IsMatch(str, @"(something|dog)*$"));
68 AssertEquals ("MEG #10", true, Regex.IsMatch(str, @"(|something|dog)*$"));
69 AssertEquals ("MEG #11", true, Regex.IsMatch(str, @"(something||dog)*$"));
70 AssertEquals ("MEG #12", true, Regex.IsMatch(str, @"(something|dog|)*$"));
75 public void Braces () // bug 52924
77 // Before the fix, the next line throws an exception
78 Regex regVar = new Regex(@"{\w+}");
79 Match m = regVar.Match ("{ }");
80 AssertEquals ("BR #01", false, m.Success);
84 public void WhiteSpaceGroupped () // bug 71077
87 string p = @"[\s\S]"; // =Category.Any
89 AssertEquals ("WSG#1", true, Regex.IsMatch (s, p));
93 public void RangeIgnoreCase() // bug 45976
95 string str = "AAABBBBAAA" ;
96 AssertEquals("RIC #01", true, Regex.IsMatch(str, @"[A-F]+", RegexOptions.IgnoreCase));
97 AssertEquals("RIC #02", true, Regex.IsMatch(str, @"[a-f]+", RegexOptions.IgnoreCase));
98 AssertEquals("RIC #03", true, Regex.IsMatch(str, @"[A-Fa-f]+", RegexOptions.IgnoreCase));
99 AssertEquals("RIC #04", true, Regex.IsMatch(str, @"[AB]+", RegexOptions.IgnoreCase));
100 AssertEquals("RIC #05", true, Regex.IsMatch(str, @"[A-B]+", RegexOptions.IgnoreCase));
103 AssertEquals("RIC #06", true, Regex.IsMatch(str, @"[A-F]+", RegexOptions.IgnoreCase));
104 AssertEquals("RIC #07", true, Regex.IsMatch(str, @"[a-f]+", RegexOptions.IgnoreCase));
105 AssertEquals("RIC #08", true, Regex.IsMatch(str, @"[A-Fa-f]+", RegexOptions.IgnoreCase));
106 AssertEquals("RIC #09", true, Regex.IsMatch(str, @"[AB]+", RegexOptions.IgnoreCase));
107 AssertEquals("RIC #10", true, Regex.IsMatch(str, @"[A-B]+", RegexOptions.IgnoreCase));
110 AssertEquals("RIC #11", true, Regex.IsMatch(str, @"[A-a]+", RegexOptions.IgnoreCase));
113 Assert("RIC #12", Regex.IsMatch(str, @"[A-a]+", RegexOptions.IgnoreCase));
118 public void Escape0 () // bug54797
120 Regex r = new Regex(@"^[\s\0]*$");
121 AssertEquals ("E0-1", true, r.Match(" \0").Success);
125 public void MultipleMatches()
127 Regex regex = new Regex (@"^(?'path'.*(\\|/)|(/|\\))(?'file'.*)$");
128 Match match = regex.Match (@"d:\Temp\SomeDir\SomeDir\bla.xml");
130 AssertEquals ("MM #01", 5, match.Groups.Count);
132 AssertEquals ("MM #02", "1", regex.GroupNameFromNumber(1));
133 AssertEquals ("MM #03", "2", regex.GroupNameFromNumber(2));
134 AssertEquals ("MM #04", "path", regex.GroupNameFromNumber(3));
135 AssertEquals ("MM #05", "file", regex.GroupNameFromNumber(4));
137 AssertEquals ("MM #06", "\\", match.Groups[1].Value);
138 AssertEquals ("MM #07", "", match.Groups[2].Value);
139 AssertEquals ("MM #08", @"d:\Temp\SomeDir\SomeDir\", match.Groups[3].Value);
140 AssertEquals ("MM #09", "bla.xml", match.Groups[4].Value);
144 public void SameNameGroups () // First problem in fixing bug #56000
146 string rex = "link\\s*rel\\s*=\\s*[\"']?alternate[\"']?\\s*";
147 rex += "type\\s*=\\s*[\"']?text/xml[\"']?\\s*href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^']*)'|(?<1>\\S+))";
148 Regex rob = new Regex (rex, RegexOptions.IgnoreCase);
152 public void UndefinedGroup () // bug 52890
154 Regex regex = new Regex( "[A-Za-z_0-9]" );
155 Match m = regex.Match( "123456789abc" );
156 Group g = m.Groups["not_defined"];
157 AssertNotNull ("#0", g);
158 AssertEquals ("#1", 0, g.Index);
159 AssertEquals ("#2", 0, g.Length);
160 AssertEquals ("#3", "", g.Value);
161 Assert ("#4", !g.Success);
162 AssertNotNull ("#5", g.Captures);
163 AssertEquals ("#6", 0, g.Captures.Count);
167 public void Quantifiers1 ()
169 Regex re = new Regex ("[\\w\\W]{8,32}");
170 Match m = re.Match (new string ('1', 7));
171 AssertEquals ("#01", false, m.Success);
175 public void Quantifiers2 ()
177 Regex re = new Regex ("[\\w\\W]{8,32}");
178 Match m = re.Match (new string ('1', 8));
179 AssertEquals ("#01", true, m.Success);
183 public void Quantifiers3 ()
185 Regex re = new Regex ("[\\w\\W]{8,32}");
186 Match m = re.Match (new string ('1', 16));
187 AssertEquals ("#01", true, m.Success);
191 public void Quantifiers4 ()
193 Regex re = new Regex ("[\\w\\W]{8,32}");
194 Match m = re.Match (new string ('1', 32));
195 AssertEquals ("#01", true, m.Success);
199 public void Quantifiers5 ()
201 Regex re = new Regex ("[\\w\\W]{8,32}");
202 Match m = re.Match (new string ('1', 33));
203 AssertEquals ("#01", true, m.Success);
207 public void CategoryAndNegated () // Was a regression after first attemp to fix 59150.
209 string text = "<?xml version=\"1.0\"?>";
210 Regex re = new Regex ("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
211 text = re.Replace (text, "{blue:<$1}{maroon:$2}{blue:$3>}");
212 AssertEquals ("#01", "{blue:<}{maroon:?xml version=\"1.0\"?}{blue:>}", text);
216 public void BackSpace ()
218 string text = "Go, \bNo\bGo" ;
219 Regex re = new Regex(@"\b[\b]");
220 text = re.Replace(text, " ");
221 AssertEquals("#01", "Go, \bNo Go", text);
225 public void ReplaceNegOneAndStartat ()
227 string text = "abcdeeee";
228 Regex re = new Regex("e+");
229 text = re.Replace(text, "e", -1, 4);
230 AssertEquals("#01", "abcde", text);
234 //[Ignore] You may want to ignore this if the bugs gets back
235 public void SplitInfiniteLoop () // bug 57274
237 string ss = "a b c d e";
238 string [] words = Regex.Split (ss, "[ \t\n\r]*");
239 AssertEquals ("#01Length", 11, words.Length);
240 AssertEquals ("#00", "", words [0]);
241 AssertEquals ("#01", "a", words [1]);
242 AssertEquals ("#02", "", words [2]);
243 AssertEquals ("#03", "b", words [3]);
244 AssertEquals ("#04", "", words [4]);
245 AssertEquals ("#05", "c", words [5]);
246 AssertEquals ("#06", "", words [6]);
247 AssertEquals ("#07", "d", words [7]);
248 AssertEquals ("#08", "", words [8]);
249 AssertEquals ("#09", "e", words [9]);
250 AssertEquals ("#10", "", words [10]);
255 public void CaseAndSearch () // bug 69065
257 string test1 = @"
\f!E ZWEITBAD :REGLER-PARAMETER 20.10.2004 SEITE 1";
258 string test2 = @" REGLER-PARAMETER ";
259 string test3 = @"REGLER-PARAMETER ";
260 Regex x = new Regex ("REGLER-PARAMETER",RegexOptions.IgnoreCase|RegexOptions.Compiled);
262 Match m = x.Match (test1);
263 AssertEquals ("#01", true, m.Success);
266 AssertEquals ("#02", true, m.Success);
269 AssertEquals ("#03", true, m.Success);
273 public void QuantifiersParseError () // bug 69193
275 Regex x = new Regex ("{1,a}");
276 x = new Regex ("{a,1}");
277 x = new Regex ("{a}");
278 x = new Regex ("{,a}");
282 public void NameLookupInEmptyMatch () // bug 74753
284 Regex regTime = new Regex (
285 @"(?<hour>[0-9]{1,2})([\:](?<minute>[0-9]{1,2})){0,1}([\:](?<second>[0-9]{1,2})){0,1}\s*(?<ampm>(?i:(am|pm)){0,1})");
287 Match mTime = regTime.Match("");
288 AssertEquals ("#01", "", mTime.Groups["hour"].Value);
289 AssertEquals ("#02", "", mTime.Groups["minute"].Value);
290 AssertEquals ("#03", "", mTime.Groups["second"].Value);
291 AssertEquals ("#04", "", mTime.Groups["ampm"].Value);
293 mTime = regTime.Match("12:00 pm");
294 AssertEquals ("#05", "12", mTime.Groups["hour"].Value);
295 AssertEquals ("#06", "00", mTime.Groups["minute"].Value);
296 AssertEquals ("#07", "", mTime.Groups["second"].Value);
297 AssertEquals ("#08", "pm", mTime.Groups["ampm"].Value);
301 public void HangingHyphens ()
304 Assert ("#01", Regex.IsMatch ("mT1[", @"m[0-9A-Za-z_-]+\["));
305 Assert ("#02", Regex.IsMatch ("mT1[", @"m[-0-9A-Za-z_]+\["));
307 Assert ("#03", Regex.IsMatch ("-a;", @"[--a]{3}"));
308 Assert ("#04", Regex.IsMatch ("-&,", @"[&--]{3}"));
310 Assert ("#05", Regex.IsMatch ("abcz-", @"[a-c-z]{5}"));
311 Assert ("#05b", !Regex.IsMatch ("defghijklmnopqrstuvwxy", @"[a-c-z]"));
313 Assert ("#06", Regex.IsMatch ("abcxyz-", @"[a-c-x-z]{7}"));
314 Assert ("#06b", !Regex.IsMatch ("defghijklmnopqrstuvw", @"[a-c-x-z]"));
316 Assert ("#07", Regex.IsMatch (" \tz-", @"[\s-z]{4}"));
317 Assert ("#07b", !Regex.IsMatch ("abcdefghijklmnopqrstuvwxy", @"[\s-z]"));
320 [Test, ExpectedException (typeof (ArgumentException))]
321 public void HangingHyphen1 ()
323 bool b = Regex.IsMatch ("foobar", @"[a-\s]");