Sample Header Ad - 728x90

Does this regex reliably remove square brackets from T-SQL?

4 votes
2 answers
2459 views
I'm using SQL Server Management Studio 17.4, which supports search-and-replace using a Microsoft version of regular expressions (regex). I need a regex expression that can reliably remove square brackets from source code, such as large CREATE TABLE statements that have been scripted from existing an database. I'm currently using this regex in the "find" dialog: \[((?!\d+)(?!PRIMARY))(([A-Z]|_|[0-9])*?)\] The "replacement" is $2. It seems to work very well, however I'm concerned this may eliminate required square brackets under some conditions. In the regex above, the (?!PRIMARY) piece ensures ON [PRIMARY] does not have the square brackets removed. Are there other exceptions I need to be aware of? I'm willing to exclude the possibility of column names being reserved words, such as [GROUP]. The environment I work in has specific naming convention that typically eliminates that possibility.
Asked by Hannah Vernon (70988 rep)
Feb 16, 2018, 07:27 PM
Last activity: Aug 20, 2018, 03:46 PM