Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用正则表达式在多行文本中查找键模式_C#_Regex_Stored Procedures - Fatal编程技术网

C# 如何使用正则表达式在多行文本中查找键模式

C# 如何使用正则表达式在多行文本中查找键模式,c#,regex,stored-procedures,C#,Regex,Stored Procedures,我使用正则表达式来解析存储过程,以便修改它们。 以下是我的示例文本: 声明@attreable表 TempID int-IDENTITY主键, AMFID smallint, AppModID smallint, AppID tinyint, 莫迪德·斯莫林, FPID smallint, URL varchar100, SortIndex smallint, [AppName]varchar100, ModName varchar100, FPName varchar100, 瓦查尔7000酒

我使用正则表达式来解析存储过程,以便修改它们。 以下是我的示例文本:

声明@attreable表 TempID int-IDENTITY主键, AMFID smallint, AppModID smallint, AppID tinyint, 莫迪德·斯莫林, FPID smallint, URL varchar100, SortIndex smallint, [AppName]varchar100, ModName varchar100, FPName varchar100, 瓦查尔7000酒店 -填写临时表格 插入@attreable 阿姆菲德, 阿普莫迪德, 阿皮德, 莫迪德, FPID, 网址, SortIndex, [AppName], ModName, FPName, WUCCDATA 选择 siAppModFP.AMFID, siAppModFP.AppModID, siAppModule.AppID, siAppModule.ModID, siAppModFP.FPID, siAppModFP.URL, siAppModFP.SortIndex, siApplication.[AppName], siModule.ModName, siFP.FPName, dbo.funcGetAppModFPWUCsiAppModFP.AMFID 从NOLOCK的应用程序

我只想得到这部分:

声明@attreable表 TempID int-IDENTITY主键, AMFID smallint, AppModID smallint, AppID tinyint, 莫迪德·斯莫林, FPID smallint, URL varchar100, SortIndex smallint, [AppName]varchar100, ModName varchar100, FPName varchar100, 瓦查尔7000酒店 请注意,它可能会重复多次。我希望文本中包含临时表的所有声明。 我使用了以下规则模式:

string re1 = "(Declare)( )(@)((?:[a-z][a-z0-9_]*))(\\s+)(Table)(\\s+)(\\(.*\\))";
Regex r = new Regex(re1, RegexOptions.Multiline | RegexOptions.IgnoreCase);
但它不起作用。 有什么想法吗?

试试这个正则表达式:

和一个示例代码:

var pattern = @"DECLARE\s+@(\w+)\s+TABLE\s+\(([^,\(\)]+(\(\d+\))?(,)?)+\)";
var matches = Regex.Matches(inputText, pattern);

foreach(Match match in matches)
    Output.Lines.Add(match.ToString());

// or in LINQ way
var result = from Match match in matches select match.ToString();
你需要得到

您必须使用单线模式,而不是多线模式

将此正则表达式与单线模式一起使用

所以,应该是这样

string re1 = @"declare\s*@(?:[a-z]\w*)\s*table.*?\)\s*\)";
Regex r = new Regex(re1 ,RegexOptions.Singleline | RegexOptions.IgnoreCase );

试试看

此工具非常适合测试具有特定模式的特定文本。您可以检查各个捕获组,右侧栏是一个regex备忘单。您使用的是多行模式,而不是单线模式。这将不起作用,因为括号可以多次打开和关闭。谢谢。。。现在工作,伟大的想法,考虑两个,而不是舍入parantheses@user1778790如果你问我如何替换,我没有理解你?它本身可以通过如下方式进行转义来替换:\?
Regex rx = new Regex(@"declare\s\@([a-zA-Z_][a-zA-Z0-9_]*)\w+table\w*\(((?<BR>\()|(?<-BR>\))|[^()]*)+\)");
declare\s*@(?:[a-z]\w*)\s*table.*?\)\s*\)
string re1 = @"declare\s*@(?:[a-z]\w*)\s*table.*?\)\s*\)";
Regex r = new Regex(re1 ,RegexOptions.Singleline | RegexOptions.IgnoreCase );