Search a String for Text or a Pattern

1. Problem

You want to determine if a string contains another string, or want to find the position of a string within another string.

2. Solution

PowerShell provides several options to help you search a string for text.

Use the –like operator to determine whether a string matches a given DOS-like wildcard:

        PS >"Hello World" -like "*llo W*"
        True

Use the –match operator to determine whether a string matches a given regular expression:

        PS >"Hello World" -match '.*l[l-z]o W.*$'
        True

Use the Contains() method to determine whether a string contains a specific string:

        PS >"Hello World".Contains("World")
        True

Use the IndexOf() method to determine the location of one string within another:

        PS >"Hello World".IndexOf("World")
        6



3. Discussion

Since PowerShell strings are fully featured .NET objects, they support many string-oriented operations directly. The Contains() and IndexOf() methods are two examples of the many features that the String class supports. To learn what other functionality the String class supports, see Section 3.9, “Learn About Types and Objects.”

Although they use similar characters, simple wildcards and regular expressions serve significantly different purposes. Wildcards are much more simple than regular expressions, and because of that, more constrained. While you can summarize the rules for wildcards in just four bullet points, entire books have been written to help teach and illuminate the use of regular expressions.

A common use of regular expressions is to search for a string that spans multiple lines. By default, regular expressions do not search across lines, but you can use the singleline (?s) option to instruct them to do so:

        PS >"Hello 'n World" -match "Hello.*World"
        False
        PS >"Hello 'n World" -match "(?s)Hello.*World"
        True

Wildcards lend themselves to simple matches, while regular expressions lend themselves to more complex matches.

For a detailed description of the –like operator, see “Comparison Operators” in Appendix A. For a detailed description of the –match operator, see “Simple Operators” in Appendix A, PowerShell Language and Environment. For a detailed list of the regular expression rules and syntax, see Appendix B, Regular Expression Reference.

One difficulty sometimes arises when you try to store the result of a PowerShell command in a string, as shown in Example 5-3.

Example 5-3. Attempting to store output of a PowerShell command in a string
PS >Get-Help Get-ChildItem
 
NAME
    Get-ChildItem
 
SYNOPSIS
    Gets the items and child items in one or more specified locations.
 
(...)
 
PS >$helpContent = Get-Help Get-ChildItem
PS >$helpContent -match "location"
False

The –match operator searches a string for the pattern you specify but seems to fail in this case. This is because all PowerShell commands generate objects. If you don’t store that output in another variable or pass it to another command, PowerShell converts to a text representation before it displays it to you. In Example 5-3, $helpContent is a fully featured object, not just its string representation:

        PS >$helpContent.Name
        Get-ChildItem

To work with the text-based representation of a PowerShell command, you can explicitly send it through the Out-String cmdlet. The Out-String cmdlet converts its input into the text-based form you are used to seeing on the screen:

        PS >$helpContent = Get-Help Get-ChildItem | Out-String
        PS >$helpContent -match "location"
        True
Tags: , , , , , , , , , , , , , ,