Regular Expressions are powerful! Programmers who use them are impressed with what they can do and...how hard it is to find out the correct pattern, only to forget about them until the next project. This post is a note to my future self with patterns I've used.

Parsing text with regular expressions in C#.

The first thing you need to test regular expression patterns is a Regex tester like regex101.com.

regex101

Patterns

The Regex engine in .NET (System.Text.RegularExpressions) evaluates a string like 20 goto 1 with a pattern like \d{0,1}. In this case the regex finds 2 matches: 10 and 20 because:

{0,1} is called a greedy pattern, which means it will return 20 instead of 2 and 0.

in C# it would look like this:

var input = "20 goto 1";
var pattern = @"\d{1,2}";
var match = Regex.Match(input, pattern);

It’s simple and powerful once you get it. I will add regex patterns to this post as I use them.

Find 1-or-2-digit decimal numbers in text

This pattern finds all occurrences of numbers between 0 and 99 in text:

\d{1,2}

Find all tags (#tag) in text

Pattern:

#(\w+)

Input:

this is a #tag and another #tag

Match information:

Match 1
Full match	10-14	`#tag`
Group 1.	11-14	`tag`

Match 2
Full match	27-31	`#tag`
Group 1.	28-31	`tag`

Match if string starts with time in 24-hour format

Pattern:

^([0-9]|[0-2][0-9]):([0-5][0-9])

Input:

12:00 Schloßpark Köpenick (until 19:00)

Match information:

Match 1
Full match	0-5	`12:00`
Group 1.	0-2	`12`
Group 2.	3-5	`00`
Written by Loek van den Ouweland on 2017-12-29.
Questions regarding this artice? You can send them to the address below.