正则表达式以匹配T-SQL变量声明 [英] Regular expression to match T-SQL variable declarations
问题描述
是否可以使用正则表达式在 declare
语句之后匹配所有T-SQL变量名称?
Is is possible, using a regular expression, to match all T-SQL variable names following a declare
statement?
在下面,我想匹配 Var1
, Var2
和 Var3
.我不想以后引用这些变量.
In the following, I'd like to match Var1
, Var2
, and Var3
. I do not want to match later references to these variables.
declare
@Var1 datetime,
@Var2 int,
@Var3 varchar(max)
我已经使用RegexBuddy进行了一段时间的研究,但似乎做得不好.我开始怀疑是否有可能.
I've been working on it for some time using RegexBuddy and can't seem to get it right. I'm starting to wonder if it's possible.
编辑:
这是我到目前为止所掌握的.它仅与第一个变量匹配.我不知道如何匹配后续的内容.
Here's what I've got so far. It only matches the first variable. I can't figure out how to match subsequent ones.
(?i:declare)\s+
(?:@([\w@$#]+)\s+(?:bigint|int|smallint|tinyint
|(?:(?:decimal|numeric)(?:\s*\(\s*\d+(?:\s*,\s*\d+)\s*\))?)
|money|smallmoney
|(?:(?:float|datetime2|datetimeoffset|time|char|varchar|nchar
|nvarchar|binary|varbinary)(?:\s*\(\s*\d+\s*\))?)
|real|date|smalldatetime|datetime|text|ntext|image
|uniqueidentifier|xml))
推荐答案
这就是我的想法,我对其进行了一些简化(省去了所有特定的数据类型),但是您可以按照自己的意愿进行修复:
Here's what I came up with, I simplified it a little (got rid of all the specific data types) but you can fix it up how you like:
(?i:declare|,)\s+?(?:(?<var>@[\w@$#]+)\s+(?<type>[\w()]+))+
关键是第一部分,您想查找关键字"declare"或",它们将成为您的分隔符.现在从技术上讲,这意味着它也可以在类似这样的东西上匹配:
The key is the very first part, you want to look for the keyword "declare" or a "," these are going to be your separators. Now techinically this means it would also match on something like this:
@var1 int,
@var2 int,
@var3 int
但是因为无论如何这将是无效的T-SQL,所以我放弃了.
But since this would be invalid T-SQL anyways, I'm letting that go.
这篇关于正则表达式以匹配T-SQL变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!