/^[-0-9a-z._~]*$/
^ :: matches the start of a string/start of a line
[ :: start of a character class, means match any one of the contained characters
- :: dash has a special meaning in character classes, so to avoid having it interpreted for its special meaning, list it first
0-9 :: shorthand for 0123456789 in a character class
a-z :: shorthand for abcdefghijklmnopqrstuvwxyz in a character class
._~ :: means exactly these characters in a character class
] :: end of the character class
* :: match zero or more of the previous atom (in this case, the character class)
$ :: matches the end of a string/end of a line
Run Code Online (Sandbox Code Playgroud)