Powershell: Select-String check pattern not work correct in this case (.,$,^)

Created on 8 Aug 2018  路  2Comments  路  Source: PowerShell/PowerShell

Actual behavior

PS C:> "Powershell" | Select-String -Pattern "."
Powershell
PS C:> "Powershell" | Select-String -Pattern "$"
Powershell
PS C:> "Powershell" | Select-String -Pattern "^"
Powershell

Area-Cmdlets-Utility Issue-Question Resolution-Answered

All 2 comments

This is by design. Those are all valid regular expressions that match.

-Pattern

Specifies the text to find. Type a string or regular expression. If you type a string, use the SimpleMatch parameter.

You can try them out here too.

If you look at the groups, you will see what is going on:

&{
"Powershell" | Select-String -Pattern "." 
"Powershell" | Select-String -Pattern "$" 
"Powershell" | Select-String -Pattern "^" 
} | % {$_.matches.groups} | ft

Groups Success Name Captures Index Length Value
------ ------- ---- -------- ----- ------ -----
{0}       True 0    {0}          0      1 P
{0}       True 0    {0}         10      0
{0}       True 0    {0}          0      0
Was this page helpful?
0 / 5 - 0 ratings