Skip to content
Matthias Bromisch edited this page Apr 26, 2023 · 3 revisions

Literal

Appends a literal string to the current pattern.

var pattern = Pattern
    .With()
    .Literal("Hello");

Result: Hello

Shorthands

Digit

Returns a pattern that matches any digit character.

var pattern = Pattern
    .With()
    .Digit();

Result: \d

Word

Returns a pattern that matches any word character.

var pattern = Pattern
    .With()
    .Word();

Result: \w

Groups

Capturing Group

Returns a pattern that matches the pattern in the group.

var pattern = Pattern
    .With()
    .Group(g => g.Digit(Quantifier.Exactly(4)));

Result: (\d{4})

Named Group

Returns a pattern that matches the pattern in the named group.

var pattern = Pattern
    .With()
    .Group("Year", g => g.Digit(Quantifier.Exactly(4)));

Result: (?<Year>\d{4})

Anchors

StartOfLine

Appends the "^" anchor to the current pattern to match the start of a line.

var pattern = Pattern
    .With()
    .StartOfLine();

Result: ^

EndOfLine

Appends the "$" anchor to the current pattern to match the end of a line.

var pattern = Pattern
    .With()
    .EndOfLine();

Result: $

OneOf

Literals

This function returns a pattern that matches the first, second, or any additional literal provided.

var pattern = Pattern
    .With()
    .OneOf("Hello", "World"));

Result: (Hello|World)

var pattern = Pattern
    .With()
    .OneOf(Quantifier.OneOrMore, "Hello", "World"));

Result: (Hello|World)+

Character Class

This function returns a pattern that matches the first, second, or any additional characters provided. It is a shorthand function for a character class.

var pattern = Pattern
    .With()
    .OneOf('a', 'b', '-', '+'));

Result: [az-+]

var pattern = Pattern
    .With()
    .OneOf(Quantifier.OneOrZero, 'a', 'b', '-', '+'));

Result: [az-+]+

Pattern

It's also possible to create a OneOf based on additional patterns.

var pattern = Pattern
    .With()
    .OneOf(
        p => p.Digit(),
        p => p.Word()
    ); 

Result: ((\d)|(\w))

Character Class

Range

Adds a range of characters to the character class.

var pattern = Pattern
    .With()
    .CharacterClass(cc => cc.Range('a', 'z'));

Result: [a-z]

var pattern = Pattern
    .With()
    .CharacterClass(Quantifier.OneOrMore, cc => cc.Range('a', 'z'));

Result: [a-z]+

OneOf

Adds a set of characters to the character class.

var pattern = Pattern
    .With()
    .CharacterClass(cc => cc.OneOf('a', 'b', '-', '+'));

Result: [az-+]

var pattern = Pattern
    .With()
    .CharacterClass(Quantifier.OneOrMore, cc => cc.OneOf('a', 'b', '-', '+'));

Result: [az-+]+

Quantifier

ZeroOrMore

Represents zero or more occurrences of the previous element.

var pattern = Pattern
    .With()
    .Digit(Quantifier.ZeroOrMore);

Result: \d*

ZeroOrMoreLazy

Represents zero or more occurrences of the previous element, matched lazily.

var pattern = Pattern
    .With()
    .Digit(Quantifier.ZeroOrMoreLazy);

Result: \d*?

OneOrMore

Represents one or more occurrences of the previous element.

var pattern = Pattern
    .With()
    .Digit(Quantifier.OneOrMore);

Result: \d+

OneOrMoreLazy

Represents one or more occurrences of the previous element, matched lazily.

var pattern = Pattern
    .With()
    .Digit(Quantifier.OneOrMoreLazy);

Result: \d+?

Optional

Represents zero or one occurrences of the previous element.

var pattern = Pattern
    .With()
    .Digit(Quantifier.Optional);

Result: \d?

Exactly

Represents exactly the specified number of occurrences of the previous element.

var pattern = Pattern
    .With()
    .Digit(Quantifier.Exactly(4));

Result: \d{4}

In the case of exactly one, the API will omit the quantifier.

var pattern = Pattern
    .With()
    .Digit(Quantifier.Exactly(1));

Result: \d

AtLeast

Represents at least the specified number of occurrences of the previous element.

var pattern = Pattern
    .With()
    .Digit(Quantifier.AtLeast(4));

Result: \d{4,}

In the case of at least zero, the API will use the quantifier for ZeroOrMore.

var pattern = Pattern
    .With()
    .Digit(Quantifier.AtLeast(0));

Result: \d*

In the case of at least one, the API will use the quantifier for OneOrMore.

var pattern = Pattern
    .With()
    .Digit(Quantifier.AtLeast(1));

Result: \d+

AtMost

Represents at most the specified number of occurrences of the previous element.

var pattern = Pattern
    .With()
    .Digit(Quantifier.AtMost(4));

Result: \d{,4}

In the case of at most one, the API will use the quantifier for Optional.

var pattern = Pattern
    .With()
    .Digit(Quantifier.AtMost(1));

Result: \d?

Range

Represents a specific range of occurrences of the preceding element.

var pattern = Pattern
    .With()
    .Digit(Quantifier.Range(1,4));

Result: \d{1,4}

In the case that min and max are equal, the API will use the quantifier for Exactly.

var pattern = Pattern
    .With()
    .Digit(Quantifier.Range(2,2));

Result: \d{2}

In the case of a range from 0 to 1, the API will use the quantifier for Optional.

var pattern = Pattern
    .With()
    .Digit(Quantifier.Range(0,1));

Result: \d?

In the case of a range from 0 to any value, the API will use the quantifier for AtLeast.

var pattern = Pattern
    .With()
    .Digit(Quantifier.Range(0,10));

Result: \d{,10}