Non-capturing groups

By default, every group is a capture group. A group can be marked as non-capturing by using ?: before the expression. In the following example, the third group has been marked as a non-capturing group:

PS> 'first second third' -match '(?<One>first) (?<Two>second) (?:third)'
True

PS> $matches

Name Value
---- -----
Two second
One first
0 first second third

The outer group, which previously added second third to the matches list, is now excluded from the results:

PS> 'first second third' -match '(first) (?:(second) (third))'; $matches
True

PS> $matches


Name Value
---- -----
3 third
2 second
1 first
0 first second third

This technique may be useful when using -replace—it simplifies the list of tokens available, even if an expression grows in complexity:

PS> 'first second third' -replace '(first) (?:(second) (third))', '$1, $2, $3'
first, second, third
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.178.240