public void Regex2_MatchAndClear() { byte[] buffer = null; // Matching should create one parsed regex Assert.True(Regex2.IsMatch(String8.Convert("aA", ref buffer), "(?i)A(?-i)A")); Assert.Single(Regex2.ParsedRegexes); // Same match should re-use Assert.True(Regex2.IsMatch(String8.Convert("AA", ref buffer), "(?i)A(?-i)A")); Assert.Single(Regex2.ParsedRegexes); // Clear should remove parsed regex Regex2.ClearRegexes(); Assert.Empty(Regex2.ParsedRegexes); // Matching should work again Assert.True(Regex2.IsMatch(String8.Convert("AA", ref buffer), "(?i)A(?-i)A")); Assert.Single(Regex2.ParsedRegexes); }
public bool IsMatch(FlexString input, string pattern, RegexOptions options = RegexOptions.None, TimeSpan timeout = default, string captureGroup = null) { return(Regex2.IsMatch(input, pattern, options)); }
public void Regex2_Basics() { byte[] buffer = null, buffer2 = null; var sample = String8.Convert("using Microsoft.VisualStudio.TestTools.UnitTesting;", ref buffer2); // Null and Empty Assert.Throws <ArgumentNullException>(() => Regex2.IsMatch(sample, null)); Assert.Throws <ArgumentNullException>(() => Regex2.IsMatch(sample, "")); Assert.False(Regex2.IsMatch(String8.Empty, "[A-Z]")); Assert.Throws <ArgumentNullException>(() => Regex2.Matches(sample, null).FirstOrDefault()); Assert.Throws <ArgumentNullException>(() => Regex2.Matches(sample, "").FirstOrDefault()); // Basic Expressions Assert.True(Regex2.IsMatch(String8.Convert("Interesting", ref buffer), "[A-Z]")); Assert.False(Regex2.IsMatch(String8.Convert("1234567890", ref buffer), "[A-Z]")); // Regex Parse Errors Assert.Throws <ArgumentException>(() => Regex2.IsMatch(sample, "(unclosedParen")); Assert.Throws <ArgumentException>(() => Regex2.IsMatch(sample, "[unclosedBrace")); Assert.Throws <ArgumentException>(() => Regex2.IsMatch(sample, "(['\"])matching quote backreference\\k1")); // Corrected Regex Parse Errors (named groups have names removed by Regex2 Assert.True(Regex2.IsMatch(sample, "(?<namedGroup>Tes)tTools")); // Match Index and Length, Multiple Matches Assert.Equal("(5, 11: ' Microsoft.'), (15, 14: '.VisualStudio.'), (28, 11: '.TestTools.'), (38, 13: '.UnitTesting;')", string.Join(", ", Regex2.Matches(sample, "[ \\.][^\\.]+[\\.;]").Select((m) => MatchToString(m, sample)))); // Overlapping Matches Assert.Equal("(5, 46: ' Microsoft.VisualStudio.TestTools.UnitTesting;'), (15, 36: '.VisualStudio.TestTools.UnitTesting;'), (28, 23: '.TestTools.UnitTesting;'), (38, 13: '.UnitTesting;')", string.Join(", ", Regex2.Matches(sample, "[ \\.].+[\\.;]").Select((m) => MatchToString(m, sample)))); // Internal String8.String8 range checks Assert.Throws <ArgumentNullException>(() => Regex2.IsMatch(new String8(null, sample.Index, sample.Length), "[A-Z]")); Assert.Throws <ArgumentOutOfRangeException>(() => Regex2.IsMatch(new String8(sample.Array, -1, sample.Length), "[A-Z]")); Assert.Throws <ArgumentOutOfRangeException>(() => Regex2.IsMatch(new String8(sample.Array, 0, sample.Length + 1), "[A-Z]")); Assert.Throws <ArgumentOutOfRangeException>(() => Regex2.IsMatch(new String8(sample.Array, sample.Length - 2, 3), "[A-Z]")); // IgnoreCase option Assert.True(Regex2.IsMatch(sample, @"[^\.]+Tools")); Assert.False(Regex2.IsMatch(sample, @"[^\.]+TOOLS")); Assert.True(Regex2.IsMatch(sample, @"[^\.]+TOOLS", RegexOptions.IgnoreCase)); // Singleline option var moreLines = String8.Convert("using Microsoft\r\n\t.VisualStudio\r\n\t.TestTools\r\n\t.UnitTesting;", ref buffer); Assert.False(Regex2.IsMatch(moreLines, "using (.+)VisualStudio")); Assert.True(Regex2.IsMatch(moreLines, "using (.+)VisualStudio", RegexOptions.Singleline)); // Multiple options Assert.True(Regex2.IsMatch(moreLines, "using (.+)VISUALSTUDIO", RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline)); // Ignored options don't throw and work with other options Assert.False(Regex2.IsMatch(sample, @"[^\.]+TOOLS", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture)); Assert.True(Regex2.IsMatch(sample, @"[^\.]+TOOLS", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture)); // Unsupported Options throw Assert.Throws <ArgumentException>(() => Regex2.IsMatch(sample, @"[^\.]+Tools", RegexOptions.Multiline)); Assert.Throws <ArgumentException>(() => Regex2.IsMatch(sample, @"[^\.]+Tools", RegexOptions.IgnorePatternWhitespace)); Assert.Throws <ArgumentException>(() => Regex2.IsMatch(sample, @"[^\.]+Tools", RegexOptions.RightToLeft)); Assert.Throws <ArgumentException>(() => Regex2.IsMatch(sample, @"[^\.]+Tools", RegexOptions.ECMAScript)); // Insensitive case Assert.True(Regex2.IsMatch(String8.Convert("aA", ref buffer), "(?i)A(?-i)A")); Assert.True(Regex2.IsMatch(String8.Convert("AA", ref buffer), "(?i)A(?-i)A")); Assert.False(Regex2.IsMatch(String8.Convert("aa", ref buffer), "(?i)A(?-i)A")); Assert.False(Regex2.IsMatch(String8.Convert("Aa", ref buffer), "(?i)A(?-i)A")); }