Monday, 5 June 2017

REGEX

Whenever you're looking for patterns inside of string values, rather then parsing the string you should generally leverage the Regex class. Listed are most of the common regex rules that you can use to leverage in your pattern matching

I generally use regex101 or another such site to build my Regex

Character escapes Character classes
  • \t - Tab
  • \r - Return
  • \n - Newline
  • \nnn - ASCII code by 2 or 3 octal digits
  • \xnn - ASCII code by 2 Hex digits
  • \unnnn - UNICODE by 4 Hex Digits
  • [chars] - matches single char inside []
  • [^chars] - matches single char not inside []
  • [first-last] - matches char in range
  • [a-z] Checks if char is a,b,c...z
  • . - wild card matches anything except line break \l
  • \w - matchs [a-zA-Z_0-9]
  • \W - matches non word char [^a-zA-Z_0-9]
  • \s - matches single whitespace char
  • \S - matches non whitespace char
  • \d - matches one digit [0-9]
  • \D - matches none digit [^0-9]
Anchors Quantifiers
  • ^ - start of line or string
  • $ - end of line or string
  • \A - start of string
  • \z - end of string
  • \Z - end of strong or before \n
  • \G - where previous match ended
  • \B - nonword boundary
  • * matches previous 0 or more
  • + matches previous 1 or more
  • ? matches previous 1 or 0 times
  • {n} matches previous n times
  • {n,} matches previous n or more times
  • {n,m} matches previous at least n at most m times inclusively
Some of the more advanced concepts such as lookbacks are not listed here.

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);
        }
    }
}


in the above example we check if text matches identically to our regex and we then extract any text that matches our regex into a collection. probably the most common uses of regex.