February 2019
Intermediate to advanced
626 pages
15h 51m
English
Capture groups can be given names. The name must be unique within the regular expression.
The following syntax is used to name a group:
(?<GroupName>Expression)
This may be applied to the previous simple example as follows:
PS> 'first second third' -match '(?<One>first) (?<Two>second) (?<Three>third)'TruePS> $matchesName Value ---- ----- One first Three third Two second 0 first second third
In PowerShell, this adds a pleasant additional capability. If the goal is to tear apart text and turn it into an object, one approach is as follows:
if ('first second third' -match '(first) (second) (third)') {
[PSCustomObject]@{
One = $matches[1]
Two = $matches[2]
Three = $matches[3]
}
}
This produces an object that contains the ...
Read now
Unlock full access