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:
Recommendation:
add System.Text.RegularExpressions to the imports:
it allows to use the shortened statement, as the namespace part can be ommited:
strNumber = System.Text.RegularExpressions. Regex.Match(“A1”,“\d”).Value
Visual
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
Kindly Note:
myMatch.Groups(0) will return the entire match
Regex Options
strValue = Regex.Match("hello","HELLO",RegexOptions.IgnoreCase).toString
strValue = Regex.Match("hello"," HELLO ",RegexOptions.IgnorePatternWhitespace Or RegexOptions.IgnoreCase).toString
Visual
Bringing RegexOptions within the statement:
strExtension = Regex.Match("abc.XLSX","(?i:\.xlsx?$)")
strFileName = Regex.Match("abc.XLSX","(?i:.*\.xlsx?$)")
Visuals
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
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)", "$1:$2")
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
Regex.Split
Regex.Split(“A1#1A2#2A3”,“#\d”)
Visuals
Some Regex Pattern Hints
- the dot
.
is representing any character, but not the \n linebreak - in Windows environments a Line break is composed by \r\n
- the defensive pattern
\r?\n
allows to get the Regex checked on online tools etc.
- the defensive pattern
- in some scenarios some characters are to escape -
"Robin *Super* Bot"
References
Docu
Online Tools:
Learning:
Questions
For questions on your RegEx case open a new topic and get individual support