Regular expression made simple in .Net
varmagicWand=Magex.New();magicWand.Character('-').Repeat.AtMostOnce().CharacterIn(Characters.Numeral).Repeat.Any().Character('.').CharacterIn(Characters.Numeral).Repeat.AtLeastOnce();// Creates a regex corresponding to// -?[0-9]*\.[0-9]+varfloatingPointNumberDetector=newRegex(magicWand.Expression);// Will match "1.234", "-1.234", "0.0", ".01"// Will not match "0" "1,234", "0x234", "#1a4f66"All of these functions are escaping special chars.
Character()matches any characterCharacter(char)matches the given characterCharacterIn(params char[])matches any character in the listCharacterIn(string)matches any character in the listCharacterIn(Characters, string)matches any character in the listCharacterIn(Characters params char[])matches any character in the listCharacterNotIn(params char[])matches any character not in the listCharacterNotIn(string)matches any character not in the listCharacterNotIn(Characters, string)matches any character not in the listCharacterNotIn(Characters params char[])matches any character not in the list
Characters is an enumeration of flags, allowing you to add special characters to the list of matched characters.
String(string)matches the given string, escaping special chars
Some content can be repeated. For this content, you can use the Repeat property and specify cardinality.
Repeat.Any()will match any number of repetitions, including 0Repeat.Once()default behaviorRepeat.Times(uint)Repeat.Between(uint, uint)Repeat.AtLeastOnce()Repeat.AtMostOnce()
Usage:
varmagicWand=Magex.New();varnotSoMagicWand=Magex.New();// You can use sub expressions, created before// or create them in placemagicWand.Group(notSoMagicWand.Character('a'));magicWand.Group(Magex.New().Character('a'));// Or use lambdasmagicWand.Group(x =>x.Character('a'));magicWand=Magex.New();// Reference a previous capture// Will match something like <strong>hello world</strong> (but please don't parse HTML with Magex in real life)magicWand.Character('<').CaptureAs("tag", x =>x.CharacterNotIn('>').Repeat.AtLeastOnce()).Character('>').Character().Repeat.Any().Lazy().String("</").BackReference("tag").Character('>');varbadHtmlTagDetector=newRegex(magicWand.Expression);// Will match "<strong>hello world</strong>", "<h1>A title</h1>"// Will not match "<h1>A tag mismatch</strong>"Group(IExpressionElement)Non capturing groupGroup(Action<IMagex>)Non capturing group defined with a lambdaCapture(IExpressionElement)Indexed captureCapture(Action<IMagex>)Indexed capture defined with a lambdaCaptureAs(string, IExpressionElement)Named captureCaptureAs(string, Action<IMagex>)Named capture defined with a lambdaBackReference(string)references a named captureBackReference(uint)references an indexed capture
Usage
varmagicWand=Magex.New();// With subexpressionsmagicWand.Alternative(Magex.New().Character('a'),Magex.New().Character('b'));varnotSoMagicWand=Magex.New();// With lambdasnotSoMagicWand.Alternative(
x =>x.Character('a'),
x =>x.Character('b'));varalternativeDetector=newRegex(notSoMagicWand.Expression);// Will match "a", "b"// Will not match "c"Alternative(params IExpressionElement[])Matches the string if it corresponds to one alternativeAlternative(params Action<IMagex>[])Same thing with
// The group will match the smallest ensemble possible, e.g. "<em>" and "</em>"varlazyMagicWand=Magex.New().Character('<').Group(x =>x.Character().Repeat.Any().Lazy()).Character('>');// The group will match the larges ensemble possible, e.g. the whole "<em>something</em>"vargreedyMagicWand=Magex.New().Character('<').Group(x =>x.Character().Repeat.Any()).Character('>');Lazy()Makes the detection lazy instead of greedy (default)
Usage:
varmagicWand=Magex.New();// Produces an expression of type (?<![1-9])([0-9]|[1-4][0-2])(?![0-9])magicWand.Builder.NumericRange(0,42);vardetector=newRegex(magicWand.Expression);// Will match "0", "9", "20", "42"// Will not match "43", "52"- 'Literal(string)' inserts a regular expression
Licensed under MIT License, Copyright 2014 Guillaume Gautreau, Timothée Bourguignon