How to Extract Strings Between Parentheses in PowerShell?

Today, I was working on a string in PowerShell where I wanted to extract strings between parentheses. I tried different methods. In this tutorial, I will show you how to extract strings between parentheses in PowerShell using various methods.

To extract strings between parentheses in PowerShell, you can use regular expressions. For example, to extract area codes from phone numbers like (123) 456-7890, you can use the pattern \((\d{3})\) with the -match operator. Here’s a sample script:

$phoneNumbers = @("(212) 555-1234", "(415) 555-5678", "(305) 555-9012")
$areaCodes = foreach ($number in $phoneNumbers) {
    if ($number -match "\((\d{3})\)") { $matches[1] }
}
Write-Output $areaCodes

This code captures the area codes enclosed in parentheses.

Particularly, I will show you three methods to extract strings between parentheses in PowerShell.

Method 1: Using Regular Expressions (Regex)

In PowerShell, you can use regular expressions to manipulate string and pattern march. Now, let me show you an example of how to extract strings between parentheses using regex.

Example 1: Extract Area Codes from Phone Numbers

Let’s say you have a list of phone numbers in the format (123) 456-7890 and you want to extract the area codes.

Here is the full script.

$phoneNumbers = @(
    "(212) 555-1234",
    "(415) 555-5678",
    "(305) 555-9012"
)

$areaCodes = foreach ($number in $phoneNumbers) {
    if ($number -match "\((\d{3})\)") {
        $matches[1]
    }
}

Write-Output $areaCodes

In this example, the regex pattern \((\d{3})\) is used to match three digits enclosed in parentheses. The -match operator checks if the string matches the pattern and $matches[1] extracts the first captured group, which is the area code.

Extract Strings Between Parentheses in PowerShell

Read Replace Special Characters in a String in PowerShell

Example 2: Extract ZIP Codes from Addresses

Consider a list of addresses where ZIP codes are enclosed in parentheses. For example, 123 Main St, Springfield, IL (62704). Now, here is the script to extract the zip code from this address.

$addresses = @(
    "123 Main St, Springfield, IL (62704)",
    "456 Elm St, Seattle, WA (98101)",
    "789 Oak St, Miami, FL (33101)"
)

$zipCodes = foreach ($address in $addresses) {
    if ($address -match "\((\d{5})\)") {
        $matches[1]
    }
}

Write-Output $zipCodes

Here, the regex pattern \((\d{5})\) is used to match five digits enclosed in parentheses. The captured ZIP codes are then outputted.

Get Strings Between Parentheses in PowerShell

Method 2: Using String Methods

Let me tell you another method.

You can use PowerShell’s string methods to find the positions of the parentheses and extract the substring between them.

Let us understand with an example.

Example 3: Extract City Names from Strings

Suppose you have a list of strings where city names are enclosed in parentheses, such as The capital of Texas is (Austin). Then, you can write the script below to get the strings between parentheses in PowerShell.

$statements = @(
    "The capital of Texas is (Austin)",
    "The capital of California is (Sacramento)",
    "The capital of New York is (Albany)"
)

$cityNames = foreach ($statement in $statements) {
    $startIndex = $statement.IndexOf('(') + 1
    $endIndex = $statement.IndexOf(')')
    $statement.Substring($startIndex, $endIndex - $startIndex)
}

Write-Output $cityNames

In this example, we use the IndexOf method to find the positions of the opening and closing parentheses. The Substring method is then used to extract the city names.

PowerShell Get Strings Between Parentheses

Method 3: Using the .NET Framework

Now, let me show you the third method, which is mostly applicable if you are working with complex string operations.

You can use the .NET Framework for more complex string manipulations. The System.Text.RegularExpressions.Regex class provides additional functionality.

Example 4: Extract State Abbreviations from Sentences

Consider a list of sentences where state abbreviations are enclosed in parentheses, such as I visited (NY) last summer. Then, you can use the below PowerShell script.

$sentences = @(
    "I visited (NY) last summer",
    "The conference was held in (CA)",
    "They moved to (TX) last year"
)

$stateAbbreviations = foreach ($sentence in $sentences) {
    [System.Text.RegularExpressions.Regex]::Match($sentence, "\((\w{2})\)").Groups[1].Value
}

Write-Output $stateAbbreviations

In this example, the System.Text.RegularExpressions.Regex class is used to match two-letter state abbreviations enclosed in parentheses. The Groups[1].Value property extracts the matched substring.

Conclusion

PowerShell provides various methods for extracting strings between parentheses. Below are complete examples of the three methods that I have explained in this example.

  • Using Regular Expressions
  • Using String Methods
  • Using the .NET Framework

I hope this will help you. If you still have any questions, please let me know in the comment section below.

You may like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.