Forum Discussion
switch matching is not sub-string matching. Indeed, by default, it is absolute matching. Thus, if I did something like this:
switch $x {
"fo" { ... }
"bar" { ... }
"foo" { ... }
}
If $x is the value "foot", none of the cases would match. If, instead, $x is "foo", only the last case would match.
The -glob flag to switch changes the matching style to "glob". With this style, any instance of an asterisk (*) matches zero or more characters, a question-mark (?) matches exactly (any) one character, and characters in square-braces ([...]) form a character class (e.g., "[adgk]" would match a single character if and only if that character is a, d, g or k).
Putting this together, in this example:
switch $x {
"foo" { ... }
"*foo" { ... }
"foo*" { ... }
"*foo*" { ... }
}
Case 1 will match if and only if $x is exactly the string "foo". Case 2 will match if $x ends with the string "foo". Case 3 will match if $x starts with the string "foo". Case 4 will match if $x contains the string "foo".
In my code:
switch -glob "[HTTP::path]" {
"*.asp" {...}
"*.aspx" {...}
the first case will match if HTTP::path ends with the string ".asp". The second case will match if HTTP::path ends with the string ".aspx".