Wednesday, July 24, 2024
HomeC#Generate strings from a Regex sample in C#

Generate strings from a Regex sample in C#

Common Expressions (aka Regex) are common as a result of it lets validate your knowledge.  Builders use them to validate enter controls for emails, cellphone numbers, or usernames.

In .NET we now have the Regex class which might validate a string primarily based on a sample.  What if you wish to generate a string from a sample, as a substitute of validating it. Then you should use Fare library

Fare library is a port of the Java library dk.brics.automaton. It makes use of finite state machines like DFA and NFA.

How one can generate strings from Regex

  1. Set up the NuGet package deal for the Fare library
    Set up-Bundle Fare
  2. Create a brand new occasion of Xeger class and begin producing patterns:
    utilizing Fare;
    Xeger xeger = new Xeger(@"^(([0-9]{3})|[0-9]{3}-)[0-9]{3}-[0-9]{4}$"); // Regex for US quantity
    for (int i = 0; i < 100; i++)

Fare library use circumstances

In the event you don’t know the place you should use it, listed below are some situations:

  • generate enter knowledge for Unit exams
  • generate legitimate random knowledge ranging from the validation guidelines

I’ve as soon as used the Fare library and the libphonenumber library to generate cellphone numbers which might be legitimate.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments