public void NonCaptureGroup_Literal() { var regex = RegularExpression.Of( NonCaptureGroup.Of(Literal.For("A")) ).ToRegex(); Assert.AreEqual("(?: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); }