[CheatSheet] - System.Text.RegularExpressions | RegEx

This CheatSheet introduces the basic use of regex functions. With further examples also special cases are presented.

Introduction

From the namespace System.Text.RegularExpressions following methods are offered:

  • Regex.Match
  • Regex.Matches
  • Regex.isMatch
  • Regex.Replace
  • Regex.Split

A simple usage for example would look like this:

grafik

Recommendation:
add System.Text.RegularExpressions to the imports:

grafik

it allows to use the shortened statement, as the namespace part can be ommited:
strNumber = System.Text.RegularExpressions. Regex.Match(“A1”,"\d").Value

Visual

grafik

Regex.Match & Regex.Matches

Samples:
strText = “Codes: a1,b1,c1”
strPattern = “([a-z])(\d)”

Getting the first Match only:
myMatch | Datatype: Match = Regex.Match(strText,strPattern)

Getting all Matches:
myMatches | DataType: MatchCollection = Regex.Matches(strText,strPattern)

Match Result retrieval

strResult = myMatch.Value

Matches Result retrieval

Within a for each loop

with LINQ

arrAllMatchValue = myMatches.Cast(Of Match).Select(function (x) x.toString).ToArray()

Direct access

strResult = myMatches(2).Value

Groups

Examples:
strText = “Codes: a1,b1,c1”
strPattern = “([a-z])(\d)”

strFirstGroupValue = myMatch.Groups(1)
arrAllFirstGroupValues = myMatches.Cast(Of Match).Select(function (x) x.Groups(1).toString).ToArray()
Visuals

grafik

Kindly Note:
myMatch.Groups(0) will return the entire match
grafik

Regex Options

strValue =  Regex.Match("hello","HELLO",RegexOptions.IgnoreCase).toString
strValue =   Regex.Match("hello"," HELLO ",RegexOptions.IgnorePatternWhitespace Or  RegexOptions.IgnoreCase).toString
Visual

grafik

General Information Access

myMatchSucess | DataType: Boolean = myMatch.Success
myMatchCount | DataType: Int32 = myMatches.Count
myMatchStartPos | DataType: Int32 = myMatch.Index

Regex.isMatch

RegEx.IsMatch("Hello", "HELLO")
RegEx.IsMatch("Hello", "HELLO", RegexOptions.IgnoreCase)
Visuals

grafik

Regex.Replace

Regex.Replace("Hello World 001","\d+","")
Regex.Replace("Hello World 001","(?<=WORLD )\d+","",RegexOptions.IgnoreCase)
Regex.Replace("Hello bot Super bot","(?<=Super )bot", Function (m) m.Value.toUpper)
Regex.Replace("A1,A2,A3","([A-Z])(\d)", Function (m) m.Groups(1).Value & ":" &  m.Groups(2).toString )
new Regex("\d\*\*").Replace("1**,2**,3**", "#",2)
new Regex("\d\*\*").Replace("1**,2**,3**", "#",1,2)
Visuals

grafik

Regex.Split

Regex.Split(“A1#1A2#2A3”,"#\d")

Visuals

grafik

References

Docu

Online Tools:

Learning:

Questions

For questions on your RegEx case open a new topic and get individual support

17 Likes

This is amazing @ppr! This is a great contribution to the forum.

I have bookmarked this post for myself :wink: and will definitely reference in my MegaPost for those who want more Regex options :smiley:

I really like how you have different sections for Regex.Replace and Regex.Split.

1 Like

Informative Peter

Thanks for sharing :blush:

1 Like

Good one @ppr
definitely going to save this!

1 Like