Question:
I’m trying to check for invalid filenames. I want a filename to only contain lowercase, uppercase, numbers, spaces, periods, underscores, dashes and parentheses. I’ve tried this regex:
1 2 3 4 5 6 7 8 |
$regex = [regex]"^([a-zA-Z0-9\s\._-\)\(]+)$" $text = "hel()lo" if($text -notmatch $regex) { write-host 'not valid' } |
I get this error:
1 2 |
Error: "parsing "^([a-zA-Z0-9\s\._-\)\(]+)$" - [x-y] range in reverse order" |
What am I doing wrong?
Answer:
Try to move the -
to the end of the character class
1 2 |
^([a-zA-Z0-9\s\._\)\(-]+)$ |
in the middle of a character class it needs to be escaped otherwise it defines a range