Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

match parentheses in powershell using regex

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:

$regex = [regex]"^([a-zA-Z0-9\s\._-\)\(]+)$"
$text = "hel()lo"

if($text -notmatch $regex)
{
    write-host 'not valid'
}

I get this error:

Error: "parsing "^([a-zA-Z0-9\s\._-\)\(]+)$" - [x-y] range in reverse order"

What am I doing wrong?

like image 437
Steve Avatar asked Jan 19 '23 17:01

Steve


1 Answers

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

^([a-zA-Z0-9\s\._\)\(-]+)$

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

like image 146
stema Avatar answered Jan 26 '23 00:01

stema