October 2017
Intermediate to advanced
440 pages
11h 47m
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 simple previous example:
PS> 'first second third' -match '(?<One>first) (?<Two>second) (?<Three>third)'; $matchesTrueName 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 result of each ...
Read now
Unlock full access