Skip to content

Repository files navigation

icon

RegexToolbox.NET Build and testPublish to NuGetNuGet Version and Downloads count

Regular expression tools for .NET developers.

RegexBuilder

RegexBuilder is a class for building regular expressions in a more human-readable way using a fluent API. It offers a number of benefits over using raw regex syntax in strings:

  • No knowledge of regular expression syntax is required: just use simple, intuitively-named classes and methods.
  • Code is easier to read, understand and maintain.
  • Code is safer and far less prone to regular expression syntax errors and programmer errors.

Here's an example:

varregex=newRegexBuilder().Text("Hello").Whitespace(RegexQuantifier.OneOrMore).Text("world!").BuildRegex();

But that's just a taste of what RegexBuilder does: for full API documentation, head over to the project wiki.

Breaking changes in 2.0

All RegexBuilder features that were deprecated in version 1.6 have been removed in 2.0.

RemovedReplaced with
StartGroup()...EndGroup()Group()
StartNamedGroup()...EndGroup()NamedGroup()
StartNonCapturingGroup()...EndGroup()NonCapturingGroup()
AddLogger()No replacement: logging has been removed.

New in 1.6

Improved grouping methods

Version 1.6 introduces a simplified syntax for cleaner, less error-prone creating of groups. Go from this:

varregex=newRegexBuilder().StartGroup().Letter().Digit().BuildRegex();// ERROR: forgot to call EndGroup()

to this:

varregex=newRegexBuilder().Group(r =>r.Letter().Digit())// Yay! Can't forget to end the group.BuildRegex();

There are also new NamedGroup() and NonCapturingGroup() methods.

The old methods StartGroup(), StartNamedGroup(), StartNonCapturingGroup() and EndGroup() have been deprecated and will be removed in version 2.0.

More powerful AnyOf() overloads

The AnyOf() method is used to match any of a set of alternatives. Previously it only let you specify strings as the alternatives, for example:

varanimalLoverRegex=newRegexBuilder().Text("I love ").AnyOf("cats","dogs","tortoises").BuildRegex();

Now you can specify arbitrarily complex sub-regexes as the alternatives, such as:

// Match 3 letters followed by a full stop OR 4 digits followed by a comma// (for some weird reason)varregex=newRegexBuilder().AnyOf(
r =>r.Letter(Exactly(3)).Text("."),
r =>r.Digit(Exactly(4)).Text(",")).BuildRegex();

New string extension method

string.Replace(Regex regex, string replacement)

Logging deprecated (removed in version 2.0)

The logging feature introduced in version 1.3 wasn't proving as useful as I'd imagined and had introduced various maintenance difficulties. In this release the APIs are deprecated and do nothing. Logging will be removed altogether in version 2.0.

Also for .NET developers

iconMimeTypes.NET: MIME type constants for your .NET projects

RegexToolbox for other languages

iconRegexToolbox for Java

iconRegexToolbox for Kotlin

iconRegexToolbox for JavaScript


RegexToolbox: Now you can be a hero without knowing regular expressions.

About

Regular expression tools for .NET developers

Topics

Resources

Stars

12 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages