我的正则表达式有问题
本文关键字:有问题 正则表达式 我的 | 更新日期: 2023-09-27 18:30:48
我的正则表达式代码不起作用,我得到的结果如下。任何想法
我的代码错误:
Getting the sentences without values like this one: CREATE SEQUENCE "" MINVALUE MAXVALUE INCREMENT BY START WITH CACHE NOORDER NOCYCLE;
更改句子的条件
- 如果 MINVALUE 在句子中具有加号值,则以与最小值...
- 如果 MINVALUE 在句子中具有负值,则以与最大值
在我的数据库中,我有 3 句话需要更改......
CREATE SEQUENCE "MY_SEQUENCE" MINVALUE -8 MAXVALUE 999 INCREMENT BY 1 START WITH 250 CACHE 50 NOORDER NOCYCLE;
CREATE SEQUENCE "_SEQUENCE" MINVALUE 151 MAXVALUE 500 INCREMENT BY 4 START WITH 160 CACHE 30 NOORDER NOCYCLE;
结果应如下所示
CREATE SEQUENCE "MY_SEQUENCE" MINVALUE -8 MAXVALUE 999 INCREMENT BY 1 START WITH 999 CACHE 50 NOORDER NOCYCLE;
CREATE SEQUENCE "_SEQUENCE" MINVALUE 151 MAXVALUE 500 INCREMENT BY 4 START WITH 151 CACHE 30 NOORDER NOCYCLE;
我的代码:
string sentence = "";
string formatprototype = "";
string output = "";
using (OracleConnection conn1 = MyConnection.GetSourceConnection())
{
conn1.Open();
using (OracleCommand crtCommand = new OracleCommand(@"MyCommand", conn1))
{
sentence = crtCommand.ExecuteScalar().ToString();
string pattern = @".*[ ]+?['""]{1}(?<String>[a-zA-Z0-9_]*)['""]{1}[ ]+?MINVALUE[ ]*(?<MinValue>[-?'d]*)[ ]*MAXVALUE[ ]*(?<MaxValue>['d]*)[ ]+?[INCREMENT]*[ ]+?[BY]*[ ]+?(?<IncrementBy>['d]*)[ ]+?[START]*[ ]+?[WITH]*[ ]+?(?<StartWith>['d]*)[ ]+?[CACHE]*[ ]+?(?<Cache>['d]*)'s+?";
Regex regex = new Regex(pattern);
Match match = regex.Match(sentence);
Group @string = match.Groups[1];
Group minvalue = match.Groups[2];
Group maxvalue = match.Groups[3];
Group incrementby = match.Groups[4];
Group startswith = match.Groups[5];
Group cache = match.Groups[6];
formatprototype = @"CREATE SEQUENCE ""{0}"" MINVALUE {1} MAXVALUE {2} INCREMENT BY {3} START WITH {4} CACHE {5} NOORDER NOCYCLE";
if (minvalue.Value.StartsWith("-"))
{
output = string.Format(formatprototype, @string, minvalue, maxvalue, incrementby, maxvalue, cache);
}
else if (!minvalue.Value.StartsWith("-"))
{
output = string.Format(formatprototype, @string, minvalue, maxvalue, incrementby, minvalue, cache);
}
MessageBox.Show(output);
}
}
}
使用此正则表达式模式
(.*?MINVALUE )(-)?(?(2)(.*?MAXVALUE ))('d+)(.*START WITH )('d+)(.*)
并替换为此模式
$1$2$3$4$5$4$7
现场演示