I generally use regex101 or another such site to build my Regex
Character escapes | Character classes |
---|---|
|
|
Anchors | Quantifiers |
|
|
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace pc.regexExample
{
class Program
{
static Random rnd = new Random(DateTime.Now.Millisecond);
static string
RandomIdNumber()
{
return $"{rnd.Next(1900, 2017)}-{rnd.Next(0, 99999)}";
}
static void Main(string[] args)
{
var IdNumber = "1984-43243";
var Ids = new string[55];
for (int i = 0; i
< Ids.Length; i++)
Ids[i] = RandomIdNumber();
var aggregatedIds = Ids.Aggregate((result, id) => $"{result};{id}");
var r = new Regex(@"^\d{4}-\d{1,5}$");
Console.WriteLine(r.IsMatch(IdNumber));
Console.WriteLine(aggregatedIds);
var results = new Regex(@"\d{4}-\d{1,5}").Matches(aggregatedIds);
foreach (var result in results)
Console.WriteLine(result);
}
}
}