PowerShell: Working With Regular Expressions (regex)

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 run down 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 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 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 different 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

 

Regular Expression  Resources

Sort by: Published Date | Most Recent | Most Useful
Comments
  • Nice set of resources learning regular expression for powershell.

  • Useful resources.

  • Thanks for sharing :)

  • I was looking for something like this, thanks for making this wiki Rich

  • jrich edited Revision 8. Comment: created basic structure, will populate data

  • jrich edited Revision 10. Comment: updated Match (saving work)

  • Very useful resources, thanks a lot

  • joakimbs edited Revision 13. Comment: Promoting my article, since I frankly think it deserves mention here.

  • Ed Price - MSFT edited Revision 15. Comment: White space tweaks

Page 1 of 1 (9 items)