PowerShell: Working With Regular Expressions (regex)
Link to Parent: [[PowerShell - Deep Dive and Best Practice]]
There are several different ways to work with regular expressions in PowerShell and this wiki will go over some of these different methods. This wiki WILL NOT go in to regex patterns, there are many resources on the web for that. If you need help with patterns check out the resource section.
**This wiki assumes basic regex knowledge**
If you need help with building RegEx patterns see the Regular Expression Resources below.
PowerShell Regex based operators
There are several different operators that support the use of regex in them. For the most part, they are fairly straight forward so this will be a quick rundown on how to use each and any neat features they might have.
Case Sensitive Matching
Each PowerShell Operator has a case sensitive version, prefixing any operator with c will make it case sensitive. They can also be prefixed with i to denote insensitively but that is the default option. Some people use it for cleaner more descriptive code. This works with all comparison operators, not just regex based operators.
"Hello Justin" -match "justin" #true, default is insensitive
"Hello Justin" -cmatch "justin" #false, case does not match
"Hello Justin" -imatch "justin" #true, explicit case insensitivity
Match
The PowerShell Match operator will return a True or False value depending on if the source matches the provided pattern. Great for use with Where or If statements.
"The number 7 is great!" -Match "\d"
There is an automatic variable created upon a match called $Matches
"Hello Justin, Welcome" -match "hello\s(\w+), welcome"
"My name is $($matches[1])"
The $Matches variable is a collection of match results from the pattern. Index 0 is the string that was matched and after that its the match group which is anything with in ( )
Replace
detail the use of -replace
String -Replace <regex pattern>[, <replacement string>]
Simple Replace - This is NOT a case sensitive match.
"hello world" -replace "world", "World"
Simple Remove
"hello world" -replace "world"
Regex Replace
"today is 04/13/1999" -replace "\d{2}/\d{2}/\d{4}", (get-date -f "MM/dd/yyyy")
Regex Replace Using Found matches
"justin.rich@technet.com" -replace "^(\w+)\.(\w+)@", '$1-$2@'
Regex replace using found matches next to numbers
"jrich532" -replace "(\d)\d{2}", "`${1}23"
Split/Join
detail use of -split and -join and how they differ from strings .split and .join
Switch Statement
detail the use of regex with the switch statement
Using the .NET regex namespace
There is a type accelerator for the .net regular expression name space [regex]
Performance Considerations
Depending on what sort of matching you need to do, there can be a very significant difference in the performance of regex. Patterns themselves can have an impact on the speed as well.
Resources
List of resources to learn about Regex for PowerShell and to practice.
PowerShell resources
- Chapter 13. Text and Regular Expressions - Master-PowerShell | With Dr. Tobias Weltner - Powershell.com
- Hey, Scripting Guy! Blog talks Regex
- A recorded presentation of Tome Tanasovski's regex talk for the UK PowerShell UserGroup
- PowerShellAdmin.com's extensive PowerShell Regex Article by Joakim Svendsen
Regular Expression Resources
- Expresso Regular Expression Tool
- Real-time Regex testing
- Cheat sheet and slide deck for Tome's regular expression presentation
- Regex cheat sheet
- Regex Reference