Tutorial by Examples

You can quickly determine if a text includes a specific pattern using Regex. There are multiple ways to work with Regex in PowerShell. #Sample text $text = @" This is (a) sample text, this is a (sample text) "@ #Sample pattern: Content wrapped in () $pattern = '\(.*?\)' Using ...
A common task for regex is to replace text that matches a pattern with a new value. #Sample text $text = @" This is (a) sample text, this is a (sample text) "@ #Sample pattern: Text wrapped in () $pattern = '\(.*?\)' #Replace matches with: $newvalue = 'test' Using -Replace...
Sometimes you need to replace a value matching a pattern with a new value that's based on that specific match, making it impossible to predict the new value. For these types of scenarios, a MatchEvaluator can be very useful. In PowerShell, a MatchEvaluator is as simple as a scriptblock with a singl...
A regex-pattern uses many special characters to describe a pattern. Ex., . means "any character", + is "one or more" etc. To use these characters, as a .,+ etc., in a pattern, you need to escape them to remove their special meaning. This is done by using the escape character whi...
There are multiple ways to find all matches for a pattern in a text. #Sample text $text = @" This is (a) sample text, this is a (sample text) "@ #Sample pattern: Content wrapped in () $pattern = '\(.*?\)' Using Select-String You can find all matches (global match) by adding t...

Page 1 of 1