0

I want to allow the dash -, _, . in the string

    $string = "atest-d56hdaff"
    
    If ($string -match "^[a-zA-Z0-9\\_\\-\\.]*$"){ 
        echo "OK"
        }
    else{
        echo "Not Ok"
        }

What am I doing wrong?

Justin
  • 297
  • 1
  • 2
  • 10
  • 1
    You do not need to double-escape the backslashes in .Net/PowerShell. `^[a-zA-Z0-9_\-\.]*$` https://regex101.com/r/LSwznn/1 – Daniel Aug 13 '22 at 07:07
  • 1
    Besides, PowerShell is case-insensitive by nature, meaning this `a-zA-Z` unnecessary as well, and the is a general regex substitute for a [word character (`\w`)](https://stackoverflow.com/q/2998519/1701026) which simplifies it to just: `^[\w\-\.]*$` and as an aside, I wonder if you actually want to allow an empty string, if not, you probably want use a plus (`+`) sign rather than an asterisk `*` sign. – iRon Aug 13 '22 at 09:25

0 Answers0