Welcome to my blog, I created this blog in order to maintain an ongoing log of my work over the years in Software Engineering to include technologies C#/VB.NET Microsoft .NET 1.1 - Current Version, SQL Server 7 - Current Version, Team Foundation Server (TFS) and HTML5. “I have not failed. I've just found 10,000 ways that won't work.” ― Thomas A. Edison
Monday, October 17, 2016
To match a string that contains only those characters (or an empty string), try
This works for .NET regular expressions, and probably a lot of other languages as well.
Breaking it down:
^: start of string[: beginning of character group
a-z : any lowercase letter
A-Z : any uppercase letter
0-9: any digit
_ : underscore
]:end of character group*: zero or more of the given characters
$ :end of string
If you don't want to allow empty strings, use + instead of *.
EDIT As others have pointed out, some regex languages have a shorthand form for [a-zA-Z0-9_]. In the .NET regex language, you can turn on ECMAScript behavior and use \w as a shorthand (yielding ^\w*$ or ^\w+$). Note that in other languages, and by default in .NET, \w is somewhat broader, and will match other sorts of unicode characters as well (thanks to Jan for pointing this out). So if you're really intending to match only those characters, using the explicit (longer) form is probably best.