lit
lit

Reputation: 16236

Why does '' not match '\s*'

Why does the empty item not match '\s*'? There are zero or more whitespace characters in the second item.

PS C:\> @('now','','is') | ?{ $_ -ne '' }
now
is
PS C:\> @('now','','is') | ?{ $_ -ne '\s*' }
now

is

Upvotes: 1

Views: 150

Answers (1)

Joey
Joey

Reputation: 354576

You would have to use the -match operator instead of the inequality operator you're currently using:

PS> @('now','','is') | ?{ $_ -match '\s*' }
now

is

However, since you're matching the empty string as well (and it may appear at any position in the input string), everything matches this regex. So perhaps you'd want to just match strings that are either empty or completely whitespace by anchoring the pattern:

PS> @('now','','is') | ?{ $_ -match '^\s*$' }

# ^ there is an empty line, it's just a bit invisible

Upvotes: 2

Related Questions