Skip Ribbon Commands
Skip to main content

Ondrej Sevecek's English Pages

:

Comments: Too much noise and will catch substrings

Engineering and troubleshooting by Directory Master!
MCM: Directory

Sorry comments are disable due to the constant load of spam

2

Title

Too much noise and will catch substrings

Author

A D R I A N

Body

You should use anchors and parentheses in your regex:
 
$optionSelected = Read-Host 'Do you want to delete the VM? (Yes/No/Y/N/0/1/True/False/T/F, default = No)'

switch -regex ($optionSelected) {

  '\A(yes|y|1|true|t)\Z' { $boolOption = $true }
  '\A(no|n|0|false|f)\Z' { $boolOption = $false }
  default { $boolOption = $false }
}

echo 'You selected: ' $boolOption

Otherwise, "yomama" or "mamayo" will be considered 'y'.

Attachments

Created at 08/03/2016 17:45 by  
Last modified at 08/03/2016 17:45 by