how can I use regex and format string argument?
I want to modify my original code in order to get sentences from database,
and changed each of them depends on the condition. How can I do that?
Inside my database I have 3 sequences 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;
CREATE SEQUENCE "MY_TEST" MINVALUE 1 MAXVALUE 888 INCREMENT BY 9 START
WITH 1 CACHE 20 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;
CREATE SEQUENCE "MY_TEST" MINVALUE 1 MAXVALUE 888 INCREMENT BY 9 START
WITH 1 CACHE 20 NOORDER NOCYCLE;
Conditions for changing the sentence
if MINVALUE has plus value inside the sentence than make start with value
same as MINVALUE...
if MINVALUE has minus value inside the sentence than make start with value
same as MAXVALUE
My Code:
//Inside my list there are sequence names to call each sequence from
database using DDL command.
private static List<string> LIST = new List<string>();
string sentence = "";
string formatprototype = "";
string output = "";
foreach (string Items in LIST)
{
using (OracleConnection conn1 =
MyConnection.GetSourceConnection(src.SrcDB, src.SrcID,
src.SrcPassword))
{
conn1.Open();
using (OracleCommand crtCommand = new
OracleCommand(@"SELECT REGEXP_REPLACE ( REPLACE (
dbms_metadata.get_ddl ('SEQUENCE', '" + Items + @"'),
'""" + src.SrcID + @""".'),'^\s+', NULL, 1, 0, 'm')
FROM dual", conn1)) //THIS COMMAND BRING EACH
SEQUENCES LIKE THIS: CREATE SEQUENCE "MY_SEQUENCE"
MINVALUE -8 MAXVALUE 999 INCREMENT BY 1 START WITH 250
CACHE 50 NOORDER NOCYCLE;
{
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);
}
}
}
Errors on my code:
Getting the sentences without values like this one: CREATE SEQUENCE ""
MINVALUE MAXVALUE INCREMENT BY START WITH CACHE NOORDER NOCYCLE;
No comments:
Post a Comment