Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression: 0-9 A-Z a-z or any of these '!#$%&'*+-/=?^_`{|}~

Tags:

c#

regex

asp.net

I need to regex string myString to only have:

  • 0-9
  • A-Z or a-z
  • any of these characters '!#$%&'*+-/=?^_`{|}~.

This is my code line:

new Regex("[a-zA-Z0-9]").IsMatch(myString);

So far I have [a-zA-Z0-9] and this works fine for the first two listitems. Currently tearing my hair out (and it's so nice I want to keep it) over metacharacters and getting nowhere.

Any help would be greatly appreciated. Thanks. Dave

like image 631
Dave Avatar asked Oct 18 '25 23:10

Dave


1 Answers

Hi there If you want only the listed characters in your string it is very simple.but you need to match beginning an end of line

new Regex("^[a-zA-Z0-9'!#$%&'*+/=?^_`{|}~.-]*$").IsMatch(myString);
like image 58
josephj1989 Avatar answered Oct 20 '25 12:10

josephj1989