RegexBugs.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // MonoTests.System.Text.RegularExpressions misc. test cases
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (c) Copyright 2003,2004 Novell, Inc. (http://www.novell.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Text.RegularExpressions;
  12. namespace MonoTests.System.Text.RegularExpressions
  13. {
  14. [TestFixture]
  15. public class RegexBugs : Assertion
  16. {
  17. [Test]
  18. public void SplitGroup () // bug51146
  19. {
  20. string [] splitResult = new Regex ("-").Split ("a-bcd-e-fg");
  21. string [] expected = new string [] {"a", "bcd", "e", "fg"};
  22. int length = expected.Length;
  23. int i;
  24. AssertEquals ("#01", length, splitResult.Length);
  25. for (i = 0; i < length; i++)
  26. AssertEquals ("#02 " + i, expected [i], splitResult [i]);
  27. splitResult = new Regex ("(-)").Split ("a-bcd-e-fg");
  28. expected = new string [] {"a", "-", "bcd", "-", "e", "-", "fg"};
  29. length = expected.Length;
  30. AssertEquals ("#03", length, splitResult.Length);
  31. for (i = 0; i < length; i++)
  32. AssertEquals ("#04 " + i, expected [i], splitResult [i]);
  33. splitResult = new Regex ("(-)b(c)").Split ("a-bcd-e-fg");
  34. expected = new string [] {"a", "-", "c", "d-e-fg" };
  35. length = expected.Length;
  36. AssertEquals ("#04", length, splitResult.Length);
  37. for (i = 0; i < length; i++)
  38. AssertEquals ("#05 " + i, expected [i], splitResult [i]);
  39. splitResult = new Regex ("-").Split ("a-bcd-e-fg-");
  40. expected = new string [] {"a", "bcd", "e", "fg", ""};
  41. length = expected.Length;
  42. AssertEquals ("#06", length, splitResult.Length);
  43. for (i = 0; i < length; i++)
  44. AssertEquals ("#07 " + i, expected [i], splitResult [i]);
  45. }
  46. [Test]
  47. public void MathEmptyGroup () // bug 42529
  48. {
  49. string str = "Match something from here.";
  50. AssertEquals ("MEG #01", false, Regex.IsMatch(str, @"(something|dog)$"));
  51. AssertEquals ("MEG #02", true, Regex.IsMatch(str, @"(|something|dog)$"));
  52. AssertEquals ("MEG #03", true, Regex.IsMatch(str, @"(something||dog)$"));
  53. AssertEquals ("MEG #04", true, Regex.IsMatch(str, @"(something|dog|)$"));
  54. AssertEquals ("MEG #05", true, Regex.IsMatch(str, @"(something|dog)*"));
  55. AssertEquals ("MEG #06", true, Regex.IsMatch(str, @"(|something|dog)*"));
  56. AssertEquals ("MEG #07", true, Regex.IsMatch(str, @"(something||dog)*"));
  57. AssertEquals ("MEG #08", true, Regex.IsMatch(str, @"(something|dog|)*"));
  58. AssertEquals ("MEG #09", true, Regex.IsMatch(str, @"(something|dog)*$"));
  59. AssertEquals ("MEG #10", true, Regex.IsMatch(str, @"(|something|dog)*$"));
  60. AssertEquals ("MEG #11", true, Regex.IsMatch(str, @"(something||dog)*$"));
  61. AssertEquals ("MEG #12", true, Regex.IsMatch(str, @"(something|dog|)*$"));
  62. }
  63. [Test]
  64. public void Braces () // bug 52924
  65. {
  66. // Before the fix, the next line throws an exception
  67. Regex regVar = new Regex(@"{\w+}");
  68. Match m = regVar.Match ("{ }");
  69. AssertEquals ("BR #01", false, m.Success);
  70. }
  71. [Test]
  72. public void WhiteSpaceGroupped () // bug 71077
  73. {
  74. string s = "\n";
  75. string p = @"[\s\S]"; // =Category.Any
  76. AssertEquals ("WSG#1", true, Regex.IsMatch (s, p));
  77. }
  78. [Test]
  79. public void RangeIgnoreCase() // bug 45976
  80. {
  81. string str = "AAABBBBAAA" ;
  82. AssertEquals("RIC #01", true, Regex.IsMatch(str, @"[A-F]+", RegexOptions.IgnoreCase));
  83. AssertEquals("RIC #02", true, Regex.IsMatch(str, @"[a-f]+", RegexOptions.IgnoreCase));
  84. AssertEquals("RIC #03", true, Regex.IsMatch(str, @"[A-Fa-f]+", RegexOptions.IgnoreCase));
  85. AssertEquals("RIC #04", true, Regex.IsMatch(str, @"[AB]+", RegexOptions.IgnoreCase));
  86. AssertEquals("RIC #05", true, Regex.IsMatch(str, @"[A-B]+", RegexOptions.IgnoreCase));
  87. str = "AaaBBBaAa" ;
  88. AssertEquals("RIC #06", true, Regex.IsMatch(str, @"[A-F]+", RegexOptions.IgnoreCase));
  89. AssertEquals("RIC #07", true, Regex.IsMatch(str, @"[a-f]+", RegexOptions.IgnoreCase));
  90. AssertEquals("RIC #08", true, Regex.IsMatch(str, @"[A-Fa-f]+", RegexOptions.IgnoreCase));
  91. AssertEquals("RIC #09", true, Regex.IsMatch(str, @"[AB]+", RegexOptions.IgnoreCase));
  92. AssertEquals("RIC #10", true, Regex.IsMatch(str, @"[A-B]+", RegexOptions.IgnoreCase));
  93. str = "Aaa[";
  94. AssertEquals("RIC #11", true, Regex.IsMatch(str, @"[A-a]+", RegexOptions.IgnoreCase));
  95. str = "Ae";
  96. Assert("RIC #12", Regex.IsMatch(str, @"[A-a]+", RegexOptions.IgnoreCase));
  97. }
  98. [Test]
  99. public void Escape0 () // bug54797
  100. {
  101. Regex r = new Regex(@"^[\s\0]*$");
  102. AssertEquals ("E0-1", true, r.Match(" \0").Success);
  103. }
  104. [Test()]
  105. public void MultipleMatches()
  106. {
  107. Regex regex = new Regex (@"^(?'path'.*(\\|/)|(/|\\))(?'file'.*)$");
  108. Match match = regex.Match (@"d:\Temp\SomeDir\SomeDir\bla.xml");
  109. AssertEquals ("MM #01", 5, match.Groups.Count);
  110. AssertEquals ("MM #02", "1", regex.GroupNameFromNumber(1));
  111. AssertEquals ("MM #03", "2", regex.GroupNameFromNumber(2));
  112. AssertEquals ("MM #04", "path", regex.GroupNameFromNumber(3));
  113. AssertEquals ("MM #05", "file", regex.GroupNameFromNumber(4));
  114. AssertEquals ("MM #06", "\\", match.Groups[1].Value);
  115. AssertEquals ("MM #07", "", match.Groups[2].Value);
  116. AssertEquals ("MM #08", @"d:\Temp\SomeDir\SomeDir\", match.Groups[3].Value);
  117. AssertEquals ("MM #09", "bla.xml", match.Groups[4].Value);
  118. }
  119. [Test]
  120. public void SameNameGroups () // First problem in fixing bug #56000
  121. {
  122. string rex = "link\\s*rel\\s*=\\s*[\"']?alternate[\"']?\\s*";
  123. rex += "type\\s*=\\s*[\"']?text/xml[\"']?\\s*href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^']*)'|(?<1>\\S+))";
  124. Regex rob = new Regex (rex, RegexOptions.IgnoreCase);
  125. }
  126. [Test]
  127. public void UndefinedGroup () // bug 52890
  128. {
  129. Regex regex = new Regex( "[A-Za-z_0-9]" );
  130. Match m = regex.Match( "123456789abc" );
  131. Group g = m.Groups["not_defined"];
  132. AssertNotNull ("#0", g);
  133. AssertEquals ("#1", 0, g.Index);
  134. AssertEquals ("#2", 0, g.Length);
  135. AssertEquals ("#3", "", g.Value);
  136. Assert ("#4", !g.Success);
  137. AssertNotNull ("#5", g.Captures);
  138. AssertEquals ("#6", 0, g.Captures.Count);
  139. }
  140. [Test]
  141. public void Quantifiers1 ()
  142. {
  143. Regex re = new Regex ("[\\w\\W]{8,32}");
  144. Match m = re.Match (new string ('1', 7));
  145. AssertEquals ("#01", false, m.Success);
  146. }
  147. [Test]
  148. public void Quantifiers2 ()
  149. {
  150. Regex re = new Regex ("[\\w\\W]{8,32}");
  151. Match m = re.Match (new string ('1', 8));
  152. AssertEquals ("#01", true, m.Success);
  153. }
  154. [Test]
  155. public void Quantifiers3 ()
  156. {
  157. Regex re = new Regex ("[\\w\\W]{8,32}");
  158. Match m = re.Match (new string ('1', 16));
  159. AssertEquals ("#01", true, m.Success);
  160. }
  161. [Test]
  162. public void Quantifiers4 ()
  163. {
  164. Regex re = new Regex ("[\\w\\W]{8,32}");
  165. Match m = re.Match (new string ('1', 32));
  166. AssertEquals ("#01", true, m.Success);
  167. }
  168. [Test]
  169. public void Quantifiers5 ()
  170. {
  171. Regex re = new Regex ("[\\w\\W]{8,32}");
  172. Match m = re.Match (new string ('1', 33));
  173. AssertEquals ("#01", true, m.Success);
  174. }
  175. [Test]
  176. public void CategoryAndNegated () // Was a regression after first attemp to fix 59150.
  177. {
  178. string text = "<?xml version=\"1.0\"?>";
  179. Regex re = new Regex ("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
  180. text = re.Replace (text, "{blue:&lt;$1}{maroon:$2}{blue:$3&gt;}");
  181. AssertEquals ("#01", "{blue:&lt;}{maroon:?xml version=\"1.0\"?}{blue:&gt;}", text);
  182. }
  183. [Test]
  184. public void BackSpace ()
  185. {
  186. string text = "Go, \bNo\bGo" ;
  187. Regex re = new Regex(@"\b[\b]");
  188. text = re.Replace(text, " ");
  189. AssertEquals("#01", "Go, \bNo Go", text);
  190. }
  191. [Test]
  192. public void ReplaceNegOneAndStartat ()
  193. {
  194. string text = "abcdeeee";
  195. Regex re = new Regex("e+");
  196. text = re.Replace(text, "e", -1, 4);
  197. AssertEquals("#01", "abcde", text);
  198. }
  199. [Test]
  200. //[Ignore] You may want to ignore this if the bugs gets back
  201. public void SplitInfiniteLoop () // bug 57274
  202. {
  203. string ss = "a b c d e";
  204. string [] words = Regex.Split (ss, "[ \t\n\r]*");
  205. AssertEquals ("#01Length", 11, words.Length);
  206. AssertEquals ("#00", "", words [0]);
  207. AssertEquals ("#01", "a", words [1]);
  208. AssertEquals ("#02", "", words [2]);
  209. AssertEquals ("#03", "b", words [3]);
  210. AssertEquals ("#04", "", words [4]);
  211. AssertEquals ("#05", "c", words [5]);
  212. AssertEquals ("#06", "", words [6]);
  213. AssertEquals ("#07", "d", words [7]);
  214. AssertEquals ("#08", "", words [8]);
  215. AssertEquals ("#09", "e", words [9]);
  216. AssertEquals ("#10", "", words [10]);
  217. }
  218. [Test]
  219. public void CaseAndSearch () // bug 69065
  220. {
  221. string test1 = @" !E ZWEITBAD :REGLER-PARAMETER 20.10.2004 SEITE 1";
  222. string test2 = @" REGLER-PARAMETER ";
  223. string test3 = @"REGLER-PARAMETER ";
  224. Regex x = new Regex ("REGLER-PARAMETER",RegexOptions.IgnoreCase|RegexOptions.Compiled);
  225. Match m = x.Match (test1);
  226. AssertEquals ("#01", true, m.Success);
  227. m = x.Match (test2);
  228. AssertEquals ("#02", true, m.Success);
  229. m = x.Match (test3);
  230. AssertEquals ("#03", true, m.Success);
  231. }
  232. [Test]
  233. public void QuantifiersParseError () // bug 69193
  234. {
  235. Regex x = new Regex ("{1,a}");
  236. x = new Regex ("{a,1}");
  237. x = new Regex ("{a}");
  238. x = new Regex ("{,a}");
  239. }
  240. [Test]
  241. public void NameLookupInEmptyMatch () // bug 74753
  242. {
  243. Regex regTime = new Regex (
  244. @"(?<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})");
  245. Match mTime = regTime.Match("");
  246. AssertEquals ("#01", "", mTime.Groups["hour"].Value);
  247. AssertEquals ("#02", "", mTime.Groups["minute"].Value);
  248. AssertEquals ("#03", "", mTime.Groups["second"].Value);
  249. AssertEquals ("#04", "", mTime.Groups["ampm"].Value);
  250. mTime = regTime.Match("12:00 pm");
  251. AssertEquals ("#05", "12", mTime.Groups["hour"].Value);
  252. AssertEquals ("#06", "00", mTime.Groups["minute"].Value);
  253. AssertEquals ("#07", "", mTime.Groups["second"].Value);
  254. AssertEquals ("#08", "pm", mTime.Groups["ampm"].Value);
  255. }
  256. }
  257. }