Skip to content

Provides a fluent API for building regular expression patterns in a readable, composable, and maintainable way. Also includes a parser that can interpret string expressions representing pattern-building code and convert them into actual pattern objects.

License

Notifications You must be signed in to change notification settings

StevenJAckerman/ReadableRex

Repository files navigation

Original Source: https://blog.flimflan.com/ReadableRegularExpressions.html

Research:

https://wirefuture.com/post/complete-guide-to-regex-in-c-patterns-groups-more

https://jack-vanlightly.com/blog/2016/2/3/creating-a-simple-tokenizer-lexer-in-c https://github.com/Vanlightly/DslParser/blob/master/DslParser/Parsing/Tokenizers/MoreEfficient/MoreEfficientRegexTokenizer.cs

https://github.com/cepicdalim/parseasy.net/blob/main/src/Parseasy/Parseasy.cs

https://github.com/ara3d/parakeet

https://code-maze.com/csharp-using-trie-class-for-efficient-text-pattern-searching/


What is this class library for, how is it built, and what are the key components ?

This class library provides a fluent API for building regular expression patterns in a readable, composable, and maintainable way. It also includes a parser that can interpret string expressions representing pattern-building code and convert them into actual pattern objects.


What is it doing?

  • Fluent Regex Construction:
    The core of the library is the Pattern class, which allows you to build up regular expressions using a chainable, object-oriented API instead of writing raw regex strings.
    Example:
var pattern = Pattern.With.StartsWith
                      .Digit.Repeat.Exactly(3)
                      .Literal("-").Repeat.Optional
                      .Digit.Repeat.Exactly(2)
                      .Literal("-").Repeat.Optional
                      .Digit.Repeat.Exactly(4)
                      .EndOfString;
// pattern.ToString() => ^\d{3}-?\d{2}-?\d{4}$
  • Expression Parsing:
    The library can also parse string expressions (like "With.StartsWith.Digit.Repeat.Exactly(3)") and turn them into equivalent Pattern objects using the PatternParser.

How is it built?

  • .NET Framework 4.8 Class Library:
    The code targets .NET Framework 4.8 and is organized as a standard C# class library.

  • Key Classes:

    • Pattern: The main builder class for regex patterns, with methods and properties for literals, character classes, quantifiers, groups, lookaheads, etc.
    • Quantifier: (Not shown in your context, but implied) Handles quantification like .Repeat.Exactly(n), .Repeat.Optional, etc.
    • Alternation: Supports alternation and conditional constructs (e.g., either/or, if/then/else).
    • PatternParser: Parses string expressions into Pattern objects using reflection and tokenization.
    • PatternParseResult: Holds the result of parsing, including success status and error messages.
  • Unit Tests:
    There are comprehensive unit tests (using MSTest) that verify the correctness of pattern construction and parsing.


Key Components

  1. Pattern Class (Fluent API)

    • Methods for appending literals, character classes, quantifiers, groups, lookaheads, etc.
    • Properties for common regex constructs (e.g., Digit, Word, StartsWith, EndOfString).
    • Supports method chaining for readable pattern construction.
  2. Alternation Class

    • Provides methods for alternation (Either) and conditional patterns (If).
  3. PatternParser

    • Tokenizes and interprets string expressions representing chained method/property calls.
    • Uses reflection to invoke the corresponding methods/properties on the Pattern class.
    • Handles method parameters, including support for params arrays and nested patterns.
  4. PatternParseResult

    • Encapsulates the result of parsing, including the constructed pattern, success flag, and error message.
  5. Unit Tests

    • Ensure that both the fluent API and the parser produce correct and equivalent regex strings.

Summary

This library enables you to build and parse regular expressions using a fluent, strongly-typed API or by parsing string expressions that mimic the fluent API.
It is built on .NET Framework 4.8, uses reflection for parsing, and is thoroughly tested with unit tests. The main components are the Pattern builder, alternation/conditional support, the parser, and supporting result types.

About

Provides a fluent API for building regular expression patterns in a readable, composable, and maintainable way. Also includes a parser that can interpret string expressions representing pattern-building code and convert them into actual pattern objects.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages