Powershell find non printing characters

36 Views Asked by At

I am attempting to validate a string to ensure it's a valid registry path, and having trouble with the non printing characters part. Given these three strings

$stringLiteral = 'line`nline'
$stringEscape = "line`nline"
$string = "lineline"

I should also find a non printing character in $stringEscape. Using a simple regex pattern that only searches for the one escaped character here works. So

$pattern = "`n"
$stringLiteral -match $pattern
$stringEscape -match $pattern
$string -match $pattern

correctly returns

False
True
False

However, I want to search on all the non printing characters defined here. So I was thinking a character set like this would work.

$pattern = '[`0`a`b`e`f`n`r`t`v]'

But that returns true for all three tests. I have also tried

$pattern = '[`0|`a|`b|`e|`f|`n|`r|`t|`v]'
$pattern = '`0`a`b`e`f`n`r`t`v'
$pattern = '`0|`a|`b|`e|`f|`n|`r|`t|`v'

None of which work, but as expected with different failure modes. What is the trick here?

0

There are 0 best solutions below