Filter array by other array using regex patterns

$items = “abt”,”about”,”after”,”again”,”air”,”all”,”along”,”also”,”an”,”and”,”another”,”in”,”year”,”you”,”your”,”was”
#about_Regular_Expressions
#http://technet.microsoft.com/en-us/library/hh847880.aspx

#$pattern = [regex] “ab[\w]{1}$”
#$pattern = “^[ai]n$”, “you*”
$pattern = “^a.[rl]{1}”, “you*”

Write-Host “— Option 1 —”
$pattern | %{ $items -match $_ }
Write-Host “—————-”
Write-Host “— Option 2 —”
$items | Select-String -Pattern $pattern | foreach-object{$_.tostring()}
Write-Host “—————-”
# Result:
# — Option 1 —
# air
# all
# you
# your
# —————-
# — Option 2 —
# air
# all
# you
# your
#—————-

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s