+ and * are two of a set of characters known as quantifiers. Quantifiers are discussed in great detail later in this chapter.
The * character can be used to repeat the preceding character zero or more times, for example:
'aaabc' -match 'a*'# Returns true, matches 'aaa'
However, zero or more means the character in question doesn't have to be present at all:
'bcd' -match 'a*' # Returns true, matches nothing
If a character must be present in a string, the + quantifier is more appropriate:
'aaabc' -match 'a+'# Returns true, matches 'aaa' 'bcd' -match 'a+' # Returns false
Combining * or + with . produces two very simple expressions: .* and .+. These expressions may be used as follows:
'Anything' -match '.*' # 0 or more. Returns true '' -match '.*' # 0 or more. Returns true 'Anything' -match '.+'# 1 or more. Returns true
Attempting to use either * or + as a match without a preceding character will result in an error:
PS> '*' -match '*'
parsing "*" - Quantifier {x,y} following nothing.
At line:1 char:1
+ '*' -match '*'
+ ~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException