C# 我的正则表达式怎么了?
我的正则表达式代码不工作,我得到的结果如下。你知道吗 “我的代码”中的错误: 改判条件 如果MINVALUE在句子中有加号,则以与相同的值开始 MINVALUE。。。 如果MINVALUE在句子中有负值,则以与相同的值开始 最大值 在我的数据库里,我有3个句子需要修改 结果应该如下所示 我的代码: 使用这个正则表达式模式C# 我的正则表达式怎么了?,c#,regex,winforms,oracle11g,format-string,C#,Regex,Winforms,Oracle11g,Format String,我的正则表达式代码不工作,我得到的结果如下。你知道吗 “我的代码”中的错误: 改判条件 如果MINVALUE在句子中有加号,则以与相同的值开始 MINVALUE。。。 如果MINVALUE在句子中有负值,则以与相同的值开始 最大值 在我的数据库里,我有3个句子需要修改 结果应该如下所示 我的代码: 使用这个正则表达式模式 (.*?MINVALUE )(-)?(?(2)(.*?MAXVALUE ))(\d+)(.*START WITH )(\d+)(.*) 并替换为这种模式 $1$2$3$4$5
(.*?MINVALUE )(-)?(?(2)(.*?MAXVALUE ))(\d+)(.*START WITH )(\d+)(.*)
并替换为这种模式
$1$2$3$4$5$4$7
@用户2760129你是什么意思?该用户发布的正则表达式似乎有效,您只需从当前代码中删除一些代码行,然后使用正则表达式。替换,这将大大减少您拥有的行数,至少在我看来,可以产生您想要的结果。@Jerry感谢您的回复。。我是否需要删除字符串模式并放入$1$2$3$4$5$4$7。。。我不明白他实际上在告诉我什么。你能告诉我我的密码吗?@user2760129当然可以。我很快就做了一个。我不知道为什么它显示为html,尽管^^;C不是我的强项。@Jerry我尝试在代码内部实现,但我做不到:var regex和var结果又到哪里去了?我是否应该去掉我的//Match=regex.Match句子@user2760129当然,我将把它放在pastebin上,因为我没有您拥有的变量:。您的问题是什么?请不要从问题中删除代码。
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