public void testFind() { String result = RE2.compile(test.pat).find(test.text); if (test.matches.Length == 0 && result.Length == 0) { // ok } else if (test.matches.Length == 0 && result.Length != 0) { Assert.Fail(String.Format("find: expected no match; got one: {0}", test)); } else if (test.matches.Length > 0 && result.Length == 0) { // Tricky because an empty result has two meanings: // no match or empty match. int[] match = test.matches[0]; if (match[0] != match[1]) { Assert.Fail(String.Format("find: expected match; got none: %s", test)); } } else { String expect = test.submatchString(0, 0); if (!expect.Equals(result)) { Assert.Fail(String.Format("find: expected {0} got {1}: {2}", expect, result, test)); } } }
// (Go: TestFindAllStringSubmatch) public void testFindAllSubmatch() { List <String[]> result = RE2.compile(test.pat).findAllSubmatch(test.text, -1); if (test.matches.Length == 0 && result == null) { // ok } else if (test.matches.Length == 0 && result != null) { Assert.Fail(String.Format("expected no match; got one: {0}", test)); } else if (test.matches.Length > 0 && result == null) { Assert.Fail(String.Format("expected match; got none: {0}", test)); } else if (test.matches.Length != result.Count) { Assert.Fail( String.Format( "expected {0} matches; got {1}: {2}", test.matches.Length, result.Count, test)); } else { for (int k = 0; k < test.matches.Length; ++k) { testSubmatch("testFindAllStringSubmatch", test, k, result[k]); } } }
// (Go: TestFindAllStringSubmatchIndex) public void testFindAllSubmatchIndex() { testFindAllSubmatchIndexCommon( "testFindAllSubmatchIndex", test, RE2.compile(test.pat).findAllSubmatchIndex(test.text, -1), false); }
// (Go: TestFindAllSubmatchIndex) public void testFindAllUTF8SubmatchIndex() { testFindAllSubmatchIndexCommon( "testFindAllUTF8SubmatchIndex", test, RE2.compile(test.pat).findAllUTF8SubmatchIndex(test.textUTF8, -1), true); }
// (Go: TestFindStringSubmatchIndex) public void testFindSubmatchIndex() { testFindSubmatchIndexCommon( "testFindStringSubmatchIndex", test, RE2.compile(test.pat).findSubmatchIndex(test.text), false); }
static void Main(string[] args) { // Cox's simple example. string text = "abbb"; string pat = "abab|abbb"; var compiled = RE2.compile(pat); var result = compiled.match(text); System.Console.WriteLine("Result of pat '" + pat + "' in text '" + text + "'" + result); }
public InterfaceData ExtractInterface(string code) { var lines = code .Split(new[] { Environment.NewLine }, StringSplitOptions.None) .SelectMany(line => line.Split('\r', '\n')) .ToList(); while (lines.Count > 0 && !RE1.IsMatch(lines[0])) { lines.RemoveAt(0); } // count the { and } characters until they match int?balance = null; for (var i = 0; i < lines.Count; i++) { var openCount = lines[i].Count(it => it == '{'); var closedCount = lines[i].Count(it => it == '}'); if (openCount > 0) { balance = balance.GetValueOrDefault() + openCount; } if (closedCount > 0) { balance = balance.GetValueOrDefault() - closedCount; } // if there's no { on the "interface ISomething" line, that doesn't mean they're balanced // we need to encounter at least one { or } before we can decide that if (balance.HasValue && balance.Value <= 0) { lines = lines.Take(i + 1).ToList(); } } var interfaceCode = string.Join(Environment.NewLine, lines); var match = RE2.Match(interfaceCode); var name = match.Groups[1].Value; return(new InterfaceData { Name = name, Code = interfaceCode }); }
// Now come the simple All cases. public void testFindAllUTF8() { List <byte[]> result = RE2.compile(test.pat).findAllUTF8(test.textUTF8, -1); if (test.matches.Length == 0 && result == null) { // ok } else if (test.matches.Length == 0 && result != null) { Assert.Fail(String.Format("findAllUTF8: expected no match; got one: {0}", test)); } else if (test.matches.Length > 0 && result == null) { throw new Exception("findAllUTF8: expected match; got none: " + test); } else { if (test.matches.Length != result.Count) { Assert.Fail( String.Format( "findAllUTF8: expected {0} matches; got {1}: {2}", test.matches.Length, result.Count), test); } for (int i = 0; i < test.matches.Length; i++) { byte[] expect = test.submatchBytes(i, 0); if (!expect.SequenceEqual(result[i])) { Assert.Fail( String.Format( "findAllUTF8: match {0}: expected {1}; got {2}: {3}", i / 2, GoTestUtils.fromUTF8(expect), GoTestUtils.fromUTF8(result[i]), test)); } } } }
// (Go: TestFindStringSubmatch) public void testFindSubmatch() { String[] result = RE2.compile(test.pat).findSubmatch(test.text); if (test.matches.Length == 0 && result == null) { // ok } else if (test.matches.Length == 0 && result != null) { Assert.Fail(String.Format("expected no match; got one: {0}", test)); } else if (test.matches.Length > 0 && result == null) { Assert.Fail(String.Format("expected match; got none: {0}", test)); } else { testSubmatch("testFindSubmatch", test, 0, result); } }
public void testFindAll() { List <String> result = RE2.compile(test.pat).findAll(test.text, -1); if (test.matches.Length == 0 && result == null) { // ok } else if (test.matches.Length == 0 && result != null) { Assert.Fail(String.Format("findAll: expected no match; got one: {0}", test)); } else if (test.matches.Length > 0 && result == null) { Assert.Fail(String.Format("findAll: expected match; got none: {0}", test)); } else { if (test.matches.Length != result.Count) { Assert.Fail( String.Format( "findAll: expected {0} matches; got {1}: {2}", test.matches.Length, result.Count, test)); } for (int i = 0; i < test.matches.Length; i++) { String expect = test.submatchString(i, 0); if (!expect.Equals(result[i])) { Assert.Fail(String.Format("findAll: expected {0}; got {1}: {2}", expect, result, test)); } } } }
// First the simple cases. public void testFindUTF8() { RE2 re = RE2.compile(test.pat); if (!re.ToString().Equals(test.pat)) { Assert.Fail(String.Format("RE2.toString() = \"{0}\"; should be \"{1}\"", re.ToString(), test.pat)); } byte[] result = re.findUTF8(test.textUTF8); if (test.matches.Length == 0 && GoTestUtils.len(result) == 0) { // ok } else if (test.matches.Length == 0 && result != null) { Assert.Fail(String.Format("findUTF8: expected no match; got one: {0}", test)); } else if (test.matches.Length > 0 && result == null) { Assert.Fail(String.Format("findUTF8: expected match; got none: {0}", test)); } else { byte[] expect = test.submatchBytes(0, 0); if (!expect.SequenceEqual(result)) { Assert.Fail( String.Format( "findUTF8: expected {0}; got {1}: {2}", GoTestUtils.fromUTF8(expect), GoTestUtils.fromUTF8(result), test)); } } }
public List <Statement> Equivalences(Statement s) { List <Statement> list = new List <Statement>(); string lisp = s.Lisp; HashSet <string> atoms = GetAtomics(s); foreach (Match m in RE1.Matches(lisp)) { GroupCollection gc = m.Groups; string[] lisps = new string[AMap2.Count]; foreach (string k in AMap1.Keys) { if (AMap2.ContainsKey(k)) { lisps[AMap2[k]] = gc[AMap1[k] + 1].Value; } } for (int i = 0; i < lisps.Length; ++i) { if (lisps[i] == null) { char c = 'A'; while (atoms.Contains(c.ToString())) { ++c; } atoms.Add(c.ToString()); lisps[i] = c.ToString(); } } Statement n = Statement.NewParse(string.Format(FS2, lisps)); if (n != null) { list.Add(n); } } atoms = GetAtomics(s); foreach (Match m in RE2.Matches(lisp)) { GroupCollection gc = m.Groups; string[] lisps = new string[AMap1.Count]; foreach (string k in AMap2.Keys) { if (AMap1.ContainsKey(k)) { lisps[AMap1[k]] = gc[AMap2[k] + 1].Value; } } for (int i = 0; i < lisps.Length; ++i) { if (lisps[i] == null) { char c = 'A'; while (atoms.Contains(c.ToString())) { ++c; } atoms.Add(c.ToString()); lisps[i] = c.ToString(); } } Statement n = Statement.NewParse(string.Format(FS1, lisps)); if (n != null) { list.Add(n); } } return(list); }
public void testFindIndex() { int[] result = RE2.compile(test.pat).findIndex(test.text); testFindIndexCommon("testFindIndex", test, result, false); }
public void testFindUTF8Index() { testFindIndexCommon( "testFindUTF8Index", test, RE2.compile(test.pat).findUTF8Index(test.textUTF8), true); }