-1

我的正则表达式代码不起作用我得到如下结果。任何想法

我的代码错误:

Getting the sentences without values like this one: CREATE SEQUENCE "" MINVALUE  MAXVALUE INCREMENT BY START WITH  CACHE  NOORDER NOCYCLE;

改句的条件

  1. 如果 MINVALUE 在句子中具有正值,则使开头的值与 MINVALUE 相同...
  2. 如果 MINVALUE 在句子中具有负值,则使开始的值与 MAXVALUE 相同

在我的数据库中,我有 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);
                    }
                }
            }
4

1 回答 1

4

使用这个 RegEx 模式

(.*?MINVALUE )(-)?(?(2)(.*?MAXVALUE ))(\d+)(.*START WITH )(\d+)(.*)

并用这种模式替换

$1$2$3$4$5$4$7

现场演示

于 2013-09-10T04:52:12.537 回答