Click here to Skip to main content
15,885,885 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to run a check against a user inputted sting that will become the name of a dynamically created table on a SQL Server database. I just wanted to make sure that I was getting this Regex correct and not missing anything. Am I covering all the bases for the identifier format rules with this expression?

C#
System.Text.RegularExpressions.Regex.Replace(tableName, @"[^\w\.@$#]", "");


It's the first time I've used a regular expression and wanted to be extra careful.
Posted

1 solution

There you will find the rules for SQL Server string identifiers:
Database Identifiers[^] (see 'Rules for Regular Identifiers' in this page).

[Correction] I did not read the code correctly. Apparently you are trying to replace all characters that are not a word, a point, a @, # or $.

I don't think a point can be part of an identifier.
 
Share this answer
 
v2
Comments
Foothill 3-Dec-15 10:56am    
That's the page I followed. I'm just trying to make sure that the Regex is done correctly or if I'm missing something.
phil.o 3-Dec-15 11:28am    
Then I don't think the "\." should be in your exclusion list. A point should be replaced by an empty string to construct a valid identifier.
Foothill 3-Dec-15 13:24pm    
That's why I asked for the review. Thank you, phil.o.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900