[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

26 Likes
How to extract some text info from a text?Can anyone help me?Please
[CheatSheet] VB.Net CheatSheet Catalogue
Get Substring from Input
Extract specific text from string
Remove Duplicate
Reqex need to get
Regex to get dollars from string
String Problem
Count of Matches
How to remove parantheses around a decimal number?
I need regular expression for below following text
Variable consists of 15 digits number, I want to read first 4 digit, next 5 digit & rest. How to read those value
Need practice test to better understanding of regex
Get the date of a text in an xml tag
Split a number after the 9th digit
Regex find
Key and value without double quotes in JSON
Email Body Extract
Need to extract information from PDF using Regular expression
Email Body Extract
Doubt with REGEX, I can't find the correct pattern
Text from notepad file
Using regular expressions in the development of UiPath
Assign activity - Object reference not set to an instance of an object
How to fetch the particular word from the sentence
Regex capturing group not working
SPLITTING STRING WITH DELIMETER
Help in extracting email
Need help to extract the value All from the paragraph
Split File Name of Excel File to create sheet name
Extract price of the array of Strings
How to get only particular field from pdf in UIpath
Data Extraction from text file
Group String in Array and Json
Regex / split without delimitter
Need to split cell info based on text start and end points
Which regex expression pattern to use if I want to select the number after 2 words?
Date extraction from PDF is not working!
How to get a dynamic Links in Mail Body content?
Regex or String manipulation needed in order to insert a new line before a value
Need help with an unusual String manipulation
How to extract the ASIN number from the amazon?
How to replace specific word using regex
How to make a replace in a string irrespective of string length?
El metodo match de regEx no acepta booleanos
How to remove character from string
REGEX CODE OUTPUT
Regex Matches Help
How to extract data from two delimiter where 2nd delimiter is bit changing
Modify only rows in Excel that contains "String"
Regex for finding Word and getting text
Splitting String at multiple words
Case insensitive regular expression validation of text field
Formatear solo la primera fila de un archivo de texto con Regex y otros
Get first bit of text on line 8
Regex format
Need to get digits
Urgent: Can anybody help with getting queue value only using regex form this text value
Help with regex expresion
How to replace string value to date format ?some times its coming without "/"
Extract data from RegEx and output to csv file
Extract words from a string
<Ultra Urgent> Extracting excel values as per requirement:
Extracting an identifier from a datatable or Excel
How Can I Get A Specific Text In The String
Which regex expression to use in order to be bale to select the second number of AUD?
Regex for date and numerical
Extraction of specific text from the Data
How to read this Sting in a regexr
Regex finding six digits
Forum Engagement Daily Reports
How to convert UiPath execution log file to Json or XML format
How do I use string manipulation to extract specific fields?
Regular expression to get number from string
Replace characters with nothing using Regular Expression pattern in Type into activity (Type this)

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