Regular expression made simple in .Net
var magicWand = 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]+
var floatingPointNumberDetector = new Regex(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:
var magicWand = Magex.New();
var notSoMagicWand = Magex.New();
// You can use sub expressions, created before
// or create them in place
magicWand.Group(notSoMagicWand.Character('a'));
magicWand.Group(Magex.New().Character('a'));
// Or use lambdas
magicWand.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('>');
var badHtmlTagDetector = new Regex(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
var magicWand = Magex.New();
// With subexpressions
magicWand.Alternative(
Magex.New().Character('a'),
Magex.New().Character('b')
);
var notSoMagicWand = Magex.New();
// With lambdas
notSoMagicWand.Alternative(
x => x.Character('a'),
x => x.Character('b')
);
var alternativeDetector = new Regex(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>"
var lazyMagicWand = 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>"
var greedyMagicWand = Magex.New().Character('<')
.Group(x => x.Character().Repeat.Any())
.Character('>');
Lazy()
Makes the detection lazy instead of greedy (default)
Usage:
var magicWand = Magex.New();
// Produces an expression of type (?<![1-9])([0-9]|[1-4][0-2])(?![0-9])
magicWand.Builder.NumericRange(0, 42);
var detector = new Regex(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