PowerShell

PowerShell and Regex (Find, Replace, Occurrences…)

Want to get more out of PowerShell? Refine the search? Just get better? Then check out Regex. Regex statements make things possible that seem impossible. In this blog post I show you a few Regex examples you can build on. Let’s dive in.

I will cover the following scenarios and issues:

  1. Find something (letters, dots …)
  2. Find and remove occurrences
  3. Find or remove German Umlauts
  4. Find e-mail addresses based on pattern

Code Sample

# Does it contain letters ? ==> '\w'

'Patrick' -match '\w'

# Does it contain digits ? ==> '/d'

'Patrick' -match '\d'

# Find a dot

'patrick.gruenauer@outlook.com' -match '\.'

'patrickgruenauer@outlookcom' -match '\.'

# Does it begin with P ? ==> '^P'

'Patrick' -match '^P'

'Patrick' -match '^a'

# Does it begin with a digit or letter ? Note: use -cmatch instead -match for case-sensitive matches

'Patrick' -cmatch '^[A-Z]'

'Patrick' -cmatch '^[a-z]'

'Patrick' -match '^[0-9]'

# Does it end with a letter ? Note: use -cmatch for case-sensitive matches

'Patrick' -cmatch '[A-Z]$'

'Patrick' -cmatch '[a-z]$'

# Remove German Umlauts

$names = 'Patrick Grünauer', 'Arnold Schwarzenegger', 'Hans Fetisch'

$names -match '[^a-zA-Z ]'

$names -match '[^a-zA-Z ]' -replace 'ü','ue' -replace 'ä','ae' -replace 'ö','oe'

# Remove every second occurence

<# 

^([^.]+.[^.]+)      ==> Create a group (first group is always $1). Capture the first two separated tokens at the start (^).
$1*                 ==> Search for group $1 and replace next character (.) with *
                    Note: [^.] means no dot.

#>

'patrick.gruenauer@outlook.com' -replace '^([^.]+.[^.]+).', '$1*'                   # replace second dot

'patrick.gruenauer@outlook.com' -replace '^([^.]+).', '$1*'                         # replace first dot

'patrick.gruenauer@outlook.com.com' -replace '^([^.]+.[^.]+.[^.]+).', '$1*'         # replace third (last) dot

'patrick.gruenauer@outlook.com.com.' -replace '[.]$', ''                            # replace dot at the end of string

$a = 'patrick.gruenauer@outlook.com.com.'
$b = [regex]::Match($a, '^([^.]+.[^.]+.[^.]+).').value
$b.substring(0,$b.length-1)                                                         # remove everthing after third dot

# Does it end with 2 or 4 letters ? ==> to find e-mail addresses ?
# Source unknown. Thanks to the unknown forum member in a regex forum

'patrick.gruenauer@outlook.com' -match '^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]{2,4}$'    

'patrick.gruenauer@outlook.domain' -match '^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]{2,4}$' 

'patrick.gruenauer@outlookdomain' -match '^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]{2,4}$' 

Hope this is helpful.

Categories: PowerShell

Tagged as: ,

3 replies »

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.