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.
User avatar
Posts: 12
Joined: Mon Jan 12, 2015 1:24 pm

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:

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


Posts: 14677
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times

Re: Powershell and Regex

Post by jvierra »

PS > 'Layout ID: LC-004' -match 'ID:\s+(.*)'
PS > $matches[1]