• Stars
    star
    122
  • Rank 290,746 (Top 6 %)
  • Language
    C#
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A library for building RegExpPattrerns

RegExpBuilder

A library for building RegExpPatterns. It is not available as a nuget package yet, because I would like to add some more features to it, before relasing a package. I'm not great at writing regex, so I would really appreciate any feedback if you have any.

What is it?

A linq extensions style of building RegExpPatterns in .NET.
Related Blogpost: I don't know Regex

Example

Good code is simple code.
Which one of these snippets of code do you want to find in your code?

var regEx = {(?:^)[A-Za-z]([A-Za-z]+|(?:\d+))(@{1,1})[A-Za-z]+(.{1,1})[A-Za-z]+(?:$)}

or

var builder = new Builder.RegExpBuilder();
var r = builder
	.StartOfInput()
    .Letter() // Must start with letter a-z
	.Letters() // any number of letters
    .Or() 
    .Digits() // any number of numbers
	.Exactly(1).Of("@")
	.Letters() // domain
	.Exactly(1).Of(".")
    .Letters() // top-level domain
    .EndOfInput()
    .ToRegExp();

How to use it

Of("Github").Or().Of("BitBucket")

        var builder = new Builder.RegExpBuilder();
        var r = builder
            .StartOfLine()
            .Exactly(1).Of("github")
            .Or()
            .Exactly(1).Of("bitbucket")
            .EndOfLine()
            .ToRegExp();
		
		// r.ToString() == "(?:^)(github{1,1}|(?:bitbucket{1,1}))(?:$)"

        Assert.IsTrue(r.Match("github").Success, "Found one Github");
        Assert.IsTrue(r.Match("bitbucket").Success, "Found one Bitbucket");

        Assert.IsFalse(r.Match("githubgithub").Success, "Oops, Found too Many Github");
        Assert.IsFalse(r.Match("bitbucketbitbucket").Success, ""Oops, Found too Many Github");

Find one digit

var builder = new Builder.RegExpBuilder();
    var r = builder
			.StartOfLine()
            .Digit()
            .EndOfLine()
            .ToRegExp();

    r.Match("1").Success; // true
	r.Match("11").Success); // false

Exactly().Of("yourString")

public void ExactlyOfCustom()
    {

        var builder = new Builder.RegExpBuilder();
        var r = builder
            .StartOfLine()
            .Exactly(3)
            .Of("a")
            .EndOfLine()
            .ToRegExp();

        Assert.IsTrue(r.Match("aaa").Success, "Three Letters");
        Assert.IsFalse(r.Match("aaaa").Success, "four Letters");
        Assert.IsFalse(r.Match("aa").Success, "two Letters");
    }

There is alot more examples in the test files!

RegExpBuilderTests.cs

More Repositories

1

ubuntuonwindows

Resources, tips, known issues etc for "Bash On Ubuntu On Windows"
296
star
2

Crowdspell

The crowdspell service client scripts
JavaScript
37
star
3

listentotheclouds

Listen, to the clouds, in realtime
HTML
28
star
4

OwinFriendlyExceptions

Owin Middleware to catch exceptions and translate them into nice HTTP responses
C#
23
star
5

LocalizedRoutes

ASP.NET Core Library and nuget package to translate routes
C#
12
star
6

PwnedPasswordsDotNetClient

A one-file implementation of the PwnedPasswords API for .NET
C#
12
star
7

Butler

Simple .NET CMS - Build your site as you wish, then add Butler to handle editable content. Read more in the readme.md
CSS
6
star
8

silly-things-you-can-do-in-html5

From my talk "A collection of very silly things you can do in HTML5 and the browser".
HTML
5
star
9

toolbox2017

This repository contains lectures and useful resource for the class of Hyper Island - Digital Data Strategist, The Toolbox module
HTML
3
star
10

open-source-diabetes

An experiment in making my diabetes open source
2
star
11

Dibs.Client

A client for DIBS payment using form POST. Includes all the .net classes used for HMAC encryption. It also have .NET classes for postmessage and callback response.
C#
2
star
12

hotkeyhero

Hotkey Hero - a browser extension for VS Code lovers
JavaScript
1
star
13

spacedog.agency

CSS
1
star
14

OwinFriendlyExceptions.Plugins

Framework plugins for OwinFriendlyExceptions
C#
1
star
15

andersaberg

Me on the internet
Astro
1
star
16

try_git

1
star
17

bw-developers

WIP
MDX
1
star
18

avpublic

CSS
1
star
19

klarna-checkout-module

Cloned from krokdeil
PHP
1
star