Book Image

Mastering Windows PowerShell Scripting

By : Brenton J.W. Blawat
Book Image

Mastering Windows PowerShell Scripting

By: Brenton J.W. Blawat

Overview of this book

Table of Contents (22 chapters)
Mastering Windows PowerShell Scripting
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Regular expressions examples


When you are developing scripts, there will be many instances where you may want to use regular expressions. This section will explore regular expressions that you may run into in your environment. Since there are many methods to creating regular expressions, these are to be used as suggested starting points for your scripts.

The following diagram shows how to evaluate a MAC Address:

To test a MAC address against a regular expression, use this syntax:

"00:a0:f8:12:34:56" -match "^([0-9a-f]{2}:){5}[0-9a-f]{2}$"

The output of this is shown in the following screenshot:

The preceding example shows how to create a regular expression to validate a MAC address. This expression leverages the –match comparison operator to evaluate whether 00:a0:f8:12:34:56 matches the expression of ^([0-9a-f]{2}:){5}[0-9a-f]{2}$. You may choose to use the –cmatch operator over the –match operator as some applications require MAC addresses to be in uppercase. When you break down the expression...