Powershell basics part 8 – Regular expressions and metacharacters


Happy new year everyone,
I hope you’ve had a great time celebrating the new years eve. In today’s post I will talk about how you can use regular expressions when working with powershell. Regular expressions are used to find patters in arrays, strings, files etc. Regular expressions are evaluated from left to right and are used with the -match operator. An example of using regular expressions with the -match operator looks like this:

if (“Happy new year” -match “Year”) 

{Write-Host “A match has been found”
}
The phrase “Happy new year” is searched from left to right in order to find a corresponding match to the word “Year”. Because the word “Year” is found in the sentence, powershell writes “A match is found”. As you can see we are looking for the “Year” word in our text and the result is true even if year is written with capital letter this is because matches are not case-sensitive. If we want to find the exact case-sensitive match we use -cmatch. Our example would be written like this:
if (“Happy new year” -cmatch “Year”)
Write-Host “A match has been found”
else
Write-Host “Negative”
}
In this case the result will be negative because “Year” is not found in our phrase.
If we want to perform a negative match search we use -notmach operator:
if (“Happy new year” -notmatch “Year”)
Write-Host “A match has been found”
else
Write-Host “Negative”
}
In this example powershell will return ” A match has been found” because it searches for the word Year and because a match is not found the result is true. If we want to use a negative match search with a case-sensitive word we use the -cnotmatch operator as in the following example:

if (“Happy new year” -cnotmatch “Year”)

Write-Host “A match has been found”
else
Write-Host “Negative”
}
As you would probably guess the result will be negative, a match will not be found.
Read about regular expressions also on microsoft website. http://social.technet.microsoft.com/wiki/contents/articles/4310.powershell-working-with-regular-expressions-regex.aspx

Powershell provided the so called metacharacters (metacharacter definition on wikipedia: http://en.wikipedia.org/wiki/Metacharacter) that helps us when we want to modify how a pattern is evaluated. In order to understand what I mean I will give you an example of using the metacharacter ^. This metacharacter is used to find a match with characters that are located at the beginning of a string. For example:

Clear-Host
if (“Happy new year” –match “^Hap”)
{
Write-Host “A match has been found”
}

A match will be found because “Hap” characters are found in the beginning of the phrase “Happy new year”. Here is the example when testing it in powershell:
If statement Powershell

I will write the list of all regular expressions characters in powershell:

Character . – matches a single characterexample:
Clear-Host
if (“Happy new year” –match “y.a.”){
Write-Host “A match has been found”
}
In this example we have to find a match with the pattern y.a. , the word year matches our search:
If statement Powershell

Character $ matches characters that are located at the end of a string:
example:
Clear-Host
if (“Happy new year” –match “ear$”)

{
Write-Host “A match has been found”
}
If statement Powershell
As you can see the characters “ear” are found at the end of our sentence “Happy new year”

Character \ matches the character that is after the character \:
example:
Clear-Host
if (“Happy new year!” –match “year\!”)

{
Write-Host “A match has been found”
}
If statement Powershell
Because the character “!” is found after the word “year” a match is found and written.

Character * matches zero or more occurrences of the preceding character:
example:
if (“Happy new year!” –match “y*”)

{
Write-Host “A match has been found”
}

If statement Powershell

Character ? matches zero or more occurrences of the preceding character:
example:
Clear-Host
if (“Happy new year!” –match “y?”)
{
Write-Host “A match has been found”
}
If statement

The metacharacters [characters] matches one or more characters that are specified between the “[” and “]”:
example:
Clear-Host
if (“Happy new year!” –match “y[ea]”)
{
Write-Host “A match has been found”
}
If statement
A match is found because both “e” and “a” characters are found in our sentence after the character “y”, in “year”

The expression [^] matches any characters except those that are written between “[” and “]”:
example:
Clear-Host
if (“Happy new year!” –match “y[^ea]r”)
{
Write-Host “A match has been found”
}

If statement
In this example because we have “e” and “a” characters between “y” and “r” in our sentence (“year”), we don’t have a match and nothing is written.

The expression [character range] returns a match when a character that is included in the range is found:example:
Clear-Host
if (“Happy new year!” –match “y[a-r]”)
{
Write-Host “A match has been found”
}

If statement

Read more about regular expressions and metacharacters on:
http://ss64.com/ps/syntax-regex.html

When we use regular expressions we can also use alternative patters to be found by using the “|” character. I will illustrate this in the next example:
Clear-Host
if (“Happy new year!” –match “ye|yea|year”)
{
Write-Host “A match has been found”
}
If statement
“a match has been found” is written because all three patters are found. Powershell first evaluates our phrase using “ye” pattern then “yea” and then “year”. When a match from our three patters is found then powershell writes “a match has been found”.

When we use regular expressions we can also use the so called regular expressions quantifiers. Quantifiers are best described on microsoft website so I will not talk about them in this post:
http://msdn.microsoft.com/en-us/library/az24scfc.aspx#quantifiers
Read also about “Character class” on microsoft website.

That’s all about powershell regular expressions, hope you’ve enjoyed it, have a nice day.

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