• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
OpenTechTips

OpenTechTips

Practical IT Guides, Expert Tips, and Real-World Solutions

MENUMENU
  • HOME
  • ALL TOPICS
    • Exchange
    • InfoSec
    • Linux
    • Networking
    • Scripting
      • PowerShell
    • SSL
    • Tools
    • Virtualization
    • Web
    • Windows
  • ABOUT
  • SUBSCRIBE
Home ยป PowerShell: Extract Pattern from a String

PowerShell: Extract Pattern from a String

August 11, 2021 - by Zsolt Agoston - last edited on August 18, 2021

In the following example we examine two ways of extracting text that matches a specific pattern from an input string. We can either use the -match operator, or the Select-String cmdlet. The former is perfectly fine if we're sure that there is only one match in the input string we process, the Select-String cmdlet can return multiple matches so it's more versatile.

We have a string with a chat dump. The dump contains attachment identifiers, like d32b83a2-3645-44f5-a4ff-b20542d080ce, the following scripts extract those for us.

Method 1 - The -match operator

The regex expression is simple, we match all patterns that starts with id=" followed by the id itself, which is 5 groups of word characters in a capturing group. Powershell handles all alphanumeric characters and underscore as word type characters.
In other words: \w = [a-zA-Z0-9_]

# Example sample
$InputStuff = 'Upload from chat<attachment id="d32b83a2-3645-44f5-a4ff-b20542d080ce"></attachment><attachment id="35182f26-2e8d-4ec4-b60b-d971b6165946"></attachment>'
$Regex = 'id="(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})'

# Method 1
$InputStuff -match $Regex
$Matches[1]
PowerShell: Extract Pattern from a String

Method 2 - With the Select-String cmdlet

The former solution returned only the first match in our source string. If we need all matches, in our case all file ids returned, we have to use Select-String with the -AllMatches switch. The command returns a set of Matches with each file ids. Those matches have two groups, we use the second group (index of 1, the first group has the index of 0) to capture out extracted file id.

# Example sample
$InputStuff = 'Upload from chat<attachment id="d32b83a2-3645-44f5-a4ff-b20542d080ce"></attachment><attachment id="35182f26-2e8d-4ec4-b60b-d971b6165946"></attachment>'
$Regex = 'id="(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})'

# Method 2
(Select-String -InputObject $InputStuff -Pattern $Regex -AllMatches).Matches | Foreach-Object {$_.Groups[1].Value}

 

PowerShell: Extract Pattern from a String

And the result:

PowerShell: Extract Pattern from a String

Reader Interactions

Comments Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tools

Secondary Sidebar

CONTENTS

  • Method 1 – The -match operator
  • Method 2 – With the Select-String cmdlet

  • Terms of Use
  • Disclaimer
  • Privacy Policy
Manage your privacy
We use technologies like cookies to store and/or access device information. We do this to improve browsing experience and to show (non-) personalized ads. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
Manage your privacy
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}