Professional Documents
Culture Documents
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.
Table of Contents
PowerShell Regex based operators
Case Sensitive Matching
Match
Replace
Split/Join
Switch Statement
Using the .NET regex namespace
Performance Considerations
Resources
PowerShell resources
Regular Expression Resources
See Also
Other Languages
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 insensitive
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 $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
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")
detail use of -split and -join and how they different from strings .split and .join
Switch Statement
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
• Recorded presentation of Tome Tanasovski's regex talk for the UK PowerShell UserGroup
• PowerShellAdmin.com's extensive PowerShell Regex Article by Joakim Svendsen
See Also
• PowerShell Portal
• Wiki: Portal of TechNet Wiki Portals
Other Languages
• PowerShell: ��य��त अ��व्य�� (regex) क� ��थ क�य� कर�� (hi-IN)