我的正则表达式出了什么问题?

时间:2013-09-10 04:22:21

标签: c# regex winforms oracle11g format-string

我的正则表达式代码无效我得到的结果如下所示。任何想法

我的代码错误:

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

更改句子的条件

  1. 如果MINVALUE在句子中的加号值比使用相同的值开始 MINVALUE ...
  2. 如果MINVALUE在句子内的值为负,则以make start为值 MAXVALUE
  3. 在我的数据库中,我有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);
                        }
                    }
                }
    

1 个答案:

答案 0 :(得分:4)

使用此RegEx模式

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

并替换为此模式

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

live Demo

相关问题