Chapter 7: Regular Expressions


Even though PowerShell is object-oriented, you probably have several administrative tasks that are more text oriented such as working with log or text files. PowerShell supports regular expressions that provide a more precise method of textual comparison. In this chapter we'll look at simple and complex textual comparison techniques.

Introduction to Regular Expressions

How often have you tried searching through log files looking for a particular piece of information; or searching for all information that meets a certain format like an IP address? A regular expression is a text pattern that is used to compare against a string of text. If the string of text matches the pattern, then you've found what you're looking for. For example, we know that an IP address has the format xxx.xxx.xxx.xxx. We don't know how many integers each octet has, only that there should be four sets of three numbers that are separated by periods. Conceptually, xxx.xxx.xxx.xxx is our pattern and if it's found in the string we are examining, a match is made. Regular expressions can be very complex and confusing at first. We don't have space in this chapter for an exhaustive review of this topic, but we will give you enough information to use basic regular expressions in your PowerShell scripts.

Up to this point pattern matching, like that covered in Chapter 6, has been pretty simple and straightforward. But what if we want to validate that a string was in a particular format such as a UNC path or an IP address? In this case we can use special regular expression characters to validate a string. Table 7.1 lists several of the more common special characters.

image from book
Table 7.1: Regular Expression Special Characters
Open table as spreadsheet

Character

Description

\w

Match a word (alpha-numeric and the underscore character)

\d

Match any digit (0–9)

\t

Match any tab

\s

Match any whitespace, tab or newline

image from book

There are additional special characters, but these are the ones you are most likely to use. By the way, each of these characters has an inverse option you can use simply by using the capital letter version. For example, if you want to match a pattern for anything that was not a digit, you would use \D. So this is an example of when PowerShell is case sensitive.

PowerShell supports the quantifiers available in .NET regular expressions as shown in Table 7.2:

image from book
Table 7.2: Regular Expression Qualifiers
Open table as spreadsheet

Format

Description

Value

Matches exact characters anywhere in the original value.

.

Matches any single character.

[value]

Matches at least one of the characters in the brackets.

[range]

Matches at least one of the characters within the range. The Use of a hyphen () allows specification of contiguous character.

[^]

Matches any character except those in brackets.

^

Matches the beginning characters.

$

Matches the end characters.

*

Matches zero or more instances of the preceding character.

?

Matches zero or more instances of the preceding character.

\

Matches the character that follows as an escaped character.

+

Matches repeating instances of the specified pattern such as abc+.

{n}

Specifies exactly n matches.

{n,}

Specifies at least n matches.

{n,m}

Specifies at least n, but no more than m matches.

image from book

By combining pattern matching characters with these quantifiers it is possible to construct some very complex regular expressions.



Windows PowerShell. TFM
Internet Forensics
ISBN: 982131445
EAN: 2147483647
Year: 2004
Pages: 289

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net