public void Backreference_Number() { var regex = RegularExpression.Of( CaptureGroup.Of(Quantifiers.OneOrMore(Literal.For("a"))), Quantifiers.ZeroOrMore(Literal.For("b")), Backreference.For(1) ).ToRegex(); Assert.AreEqual(@"(a+)b*\1", regex.ToString()); }
public void Alternation_ByCapture() { var regex = RegularExpression.Of( CaptureGroup.Of(new CaptureGroupOptions() { Name = "A" }, Quantifiers.OneOrMore(Literal.For("A"))), ConditionalAlternation.For(Literal.For("A"), Literal.For("Abc"), Literal.For("Xyz")) ).ToRegex(); Assert.AreEqual("(?<A>A+)(?(A)Abc|Xyz)", regex.ToString()); }
public void Backreference_Name_WithQuotes() { var regex = RegularExpression.Of( CaptureGroup.Of(new CaptureGroupOptions() { Name = "a", UseQuotes = true }, Quantifiers.OneOrMore(Literal.For("a"))), Quantifiers.ZeroOrMore(Literal.For("b")), Backreference.For("a", true) ).ToRegex(); Assert.AreEqual(@"(?'a'a+)b*\k'a'", regex.ToString()); }
public void Backreference_Name() { var regex = RegularExpression.Of( CaptureGroup.Of(new CaptureGroupOptions() { Name = "a" }, Quantifiers.OneOrMore(Literal.For("a"))), Quantifiers.ZeroOrMore(Literal.For("b")), Backreference.For("a") ).ToRegex(); Assert.AreEqual(@"(?<a>a+)b*\k<a>", regex.ToString()); }
public void UrlScheme() { var expression = RegularExpression.Of( Anchors.Carot, // scheme - (?:([A-Za-z]+):)? Quantifiers.ZeroOrOne( NonCaptureGroup.Of( CaptureGroup.Of( Quantifiers.OneOrMore( CharacterGroup.Of( Range.For(Literal.For('A'), Literal.For('Z')), Range.For(Literal.For('a'), Literal.For('z')) ) ) ), Literal.For(":") ) ), // slash - (/{0,3}) CaptureGroup.Of( Quantifiers.Between(Literal.For("/"), 0, 3) ), // host - ([0-9.\-A-Za-z]+) CaptureGroup.Of( Quantifiers.OneOrMore( CharacterGroup.Of( Range.For(Literal.For('0'), Literal.For('9')), Literal.For("."), Literal.For("-"), Range.For(Literal.For('A'), Literal.For('Z')), Range.For(Literal.For('a'), Literal.For('z')) ) ) ), // port - (?::(\d+))? Quantifiers.ZeroOrOne( NonCaptureGroup.Of( Literal.For(":"), CaptureGroup.Of( Quantifiers.OneOrMore(CharacterClasses.Digit) ) ) ), // path - (/[^?#]*)? Quantifiers.ZeroOrOne( CaptureGroup.Of( Literal.For("/"), Quantifiers.ZeroOrMore( CharacterGroup.Of( new CharacterGroupOptions() { IsNegated = true }, Literal.For("?"), Literal.For("#") ) ) ) ), // query - (?:\?([^#]*))? Quantifiers.ZeroOrOne( NonCaptureGroup.Of( Literal.For("?"), CaptureGroup.Of( Quantifiers.ZeroOrMore( CharacterGroup.Of(new CharacterGroupOptions() { IsNegated = true }, Literal.For("#")) ) ) ) ), // hash - (?:#(.*))? Quantifiers.ZeroOrOne( NonCaptureGroup.Of( Literal.For("#"), CaptureGroup.Of( Quantifiers.ZeroOrMore(CharacterClasses.Wildcard) ) ) ), Anchors.Dollar ); var regex = expression.ToRegex(); var source = regex.ToString(); const string urlRegex = @"^(?:([A-Za-z]+):)?(/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(/[^?#]*)?(?:\?([^#]*))?(?:\#(.*))?$"; Assert.AreEqual(urlRegex, source); }