Thursday, 22 August 2013

Regex to match SQL Server field name that should be delimited

Regex to match SQL Server field name that should be delimited

I want to write a regex in VB.NET that takes a string and determines
whether it should be delimited for it to be valid as a field name in SQL
server.
For example, myField is valid (example context: SELECT myField from
myTable), but my field is not and would need to be delimited with square
brackets (select [my field] from myTable).
The regex should match a string containing any of the following:
Whitespace characters
Special characters (!"£$%^ etc), not including _, @, #
String starting with a number or @
Any other field naming rules (not including sql reserved keywords, a
separate function deals with that)
My current regex pattern is [^A-Za-z]+, which thereabouts works as it
matches any non-alphabetical character, but it unecessarily matches names
such as my_field or field0

No comments:

Post a Comment