Domanda

My regex code doesnt work I am getting result like below.Any Idea

Errors on my code:

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

Conditions for changing the sentence

  1. if MINVALUE has plus value inside the sentence than make start with value same as MINVALUE...
  2. if MINVALUE has minus value inside the sentence than make start with value same as MAXVALUE

Inside my database I have 3 sentence that needs to be change...

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;

Result should be like below

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;    

My Code:

            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);
                    }
                }
            }
È stato utile?

Soluzione

use this RegEx pattern

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

and replace with this pattern

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

live Demo

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top