The first thing you need to test regular expression patterns is a Regex tester like regex101.com.
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:
\d
is a decimal{0,1}
takes either 1 or 2 decimals{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.
This pattern finds all occurrences of numbers between 0 and 99 in text:
\d{1,2}
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`
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`