Powershell and Regex

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
cstewart28
Posts: 12
Last visit: Thu Sep 01, 2016 8:41 am

Powershell and Regex

Post by cstewart28 »

I have a document that has a structured layout, such as:

Layout ID: LC-004
Name: Items 1
Description: Description of stuff
Type: Server
Equipment ID: 3sdrsqq
PPEquipment ID: PP-003
IP Address: 10.0.0.1

This is on a couple pages, I need to put all the values into an array

Here is here I'm starting from:

$file = get-content "C:\tools\Configuration.htm"

$regex = "^Layout ID:\s*(.+)"

$ID = ([regex]::Matches($file,$regex) | %{$_.value})

The result I get for $ID is Layout ID: LC-004 when all I want is LC-004

Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell and Regex

Post by jvierra »

PS > 'Layout ID: LC-004' -match 'ID:\s+(.*)'
True
PS > $matches[1]
LC-004
This topic is 7 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked