match parentheses in powershell using regex

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:

I get this error:

What am I doing wrong?

Answer:

Try to move the - to the end of the character class

in the middle of a character class it needs to be escaped otherwise it defines a range

Source:

match parentheses in powershell using regex by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply