Skip to content

Regextra simplifies some tasks typically solved via regex so that you no longer have (problems){2}

License

Notifications You must be signed in to change notification settings

amageed/Regextra

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Regextra

Regextra is a small library built to address problems that are handily solved by regular expressions.

Currently, the library includes the following features:

  • Passphrase Regex Builder
  • Named Template Formatting
  • Useful regex/string methods

How do I get started?

  • Check out the wiki
  • Visit the project's demo site for a chance to try out some client-side validation (using the patterns produced by the PassphraseRegex builder)
  • The extensive test suite is worth a glance

Regextra is available via NuGet:

PM> Install-Package Regextra 

Passphrase Regex Builder

A common question I've seen on StackOverflow is how to write code that enforces strong passphrase or password rules. Popular responses tend to tackle the problem by using a regex with look-aheads. I've seen this so much that I decided to have fun writing a solution that allowed people to produce regex patterns that would enforce such rules.

Example usage

The following code generates a pattern to enforce a password of 8-25 characters that requires at least two lowercase letters in the range of a-z and numbers excluding those in the range of 0-4 (i.e., numbers in the 5-9 range are acceptable).

var builder = PassphraseRegex.With.MinLength(8)
                                  .MaxLength(25)
                                  .IncludesRange('a', 'z')
                                  .WithMinimumOccurrenceOf(2)
                                  .ExcludesRange(0, 4);

PassphraseRegexResult result = builder.ToRegex();

if (result.IsValid)
{
    if (result.Regex.IsMatch(input))
    {
        // passphrase meets requirements
    }
    else
    {
        // passphrase is no good
    }
}
else
{
    // check the regex parse exception message for the generated pattern
    Console.WriteLine(result.Error);
}

Refer to the PassphraseRegex wiki for further details and examples.

Template Formatting

Template formatting allows you to perform named formatting on a string template using an object's matching properties. It's available via the static Template.Format method and the string extension method, FormatTemplate. The formatter features:

  • Nested properties formatting
  • Dictionary formatting
  • Standard/Custom string formatting
  • Escaping of properties
  • Detailed exception messages to pinpoint missing properties
  • Great performance (in part thanks to FastMember)

Example usage

var order = new
{
    Description = "Widget",
    OrderDate = DateTime.Now,
    Details = new
    {
        UnitPrice = 1500
    }
};

string template = "We just shipped your order of '{Description}', placed on {OrderDate:d}. Your {{credit}} card will be billed {Details.UnitPrice:C}.";

string result = Template.Format(template, order);
// or use the extension: template.FormatTemplate(order);

The result of the code is:

We just shipped your order of 'Widget', placed on 2/28/2014. Your {credit} card will be billed $1,500.00.

Refer to the Template wiki for further details and examples.

RegexUtility Class

This static class features a couple of helpful methods, such as:

  • Split Methods

    • Split
    • SplitRemoveEmptyEntries
    • SplitIncludeDelimiters
    • SplitMatchWholeWords
    • SplitTrimWhitespace
  • Formatting Methods

    • TrimWhitespace
    • FormatCamelCase
  • Named Groups Conversion Methods

    • MatchesToNamedGroupsDictionaries
    • MatchesToNamedGroupsLookup

Refer to the RegexUtility wiki for further details and examples.

OSS Libraries Used

Regextra makes use of the following OSS libraries:

Core Contributor


Regextra is Copyright © 2013-2014 Ahmad Mageed under the MIT license.

About

Regextra simplifies some tasks typically solved via regex so that you no longer have (problems){2}

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages