简体   繁体   中英

Need help in regex function in PowerShell

I've below sample text in a text file and wanted to get the value after id to be captured in a variable for later use.

id                          : 42cdb643-0a11-416b-b66b-7bb28a9a20ea
isReadOnly                  : False
somestring                   : False
somestring                   : {}
name                        : 

id                          : d006d69e-8d04-48d7-8806-cdc39c18e679
somestring                  : False
somestring                  : False
somestring                  : 822F0340-818A-4F23-AA78-633B6436913F
upstreamDataflowsOfDatasets : {}

I have the same set of data in the text file. I'm looking for a PowerShell code which will scan the text and return value for id as 42cdb643-0a11-416b-b66b-7bb28a9a20ea and so on.

Maybe,

\bid\s+:\s*\b([0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12})\b

would probably work OK, and your desired id value is in the capturing group $1 .

Demo


If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com . If you'd like, you can also watch in this link , how it would match against some sample inputs.


RegEx Circuit

jex.im visualizes regular expressions:

在此处输入图像描述

Source

To get all the id property values from your text file, use a relatively simple regex with the Select-String cmdlet:

Select-String '^id\s+:\s+(.*)' File.txt | foreach { $_.Matches.Groups[1].Value }

With your sample text, this yields (an array of [string] s):

42cdb643-0a11-416b-b66b-7bb28a9a20ea
d006d69e-8d04-48d7-8806-cdc39c18e679

Explanation:

  • Regex ^id\s+:\s+(.*) finds lines that start with ( ^ ) string id , followed by one or more whitespace chars. ( \s+ ), a : and more whitespace, and captures whatever remains on the line ( .* ) in a capture group ( (...) ).

  • Select-String passes only matching lines through.

  • The foreach ( ForEach-Object ) command accesses the details of the regex match ( $_.Matches ), whose .Groups property contains the capture-group matches; index [1] is the first (and only) capture group's match, and its .Value property contains the matched text, which is the ID of interest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM