Recovery of a string between two words
I'd like to pick up a string into a text file. The string that I want to recover begins with ALTER TABLE and ends by GO.
Code:
ALTER TABLE [T_FA] WITH CHECK ADD FOREIGN KEY ([ID_MD])
REFERENCES [T_MD] ([ID_MD])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
I tried several regular expressions but I can not find the right one. I found that it works but only if GO is on the second line ...
Code:
Regex maregex = new Regex (@ "ALTER TABLE (? <test> (.*)( \ S *)) GO");
How do I make my regex search goes on GO after several line breaks?
Re: Recovery of a string between two words
For research on multiple lines, overloads your builder with regex "RegexOptions.Singleline", so the "." same match any line breaks, which simplifies things a lot.
Code:
Regex taregex = new Regex(@"ALTER TABLE(?<test>.*)GO", RegexOptions.Singleline);
If you want the complete sql query in Groups [ "test"]:
Code:
Regex taregex2 = new Regex(@"(?<test>ALTER TABLE.*GO)", RegexOptions.Singleline);
Re: Recovery of a string between two words
Thank you for your reply, however, an error may occur. Assuming that my connection string is like this:
Code:
ALTER TABLE [T_AA] WITH CHECK ADD FOREIGN KEY([ID_BB])
REFERENCES [T_BB] ([ID_BB])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [T_FA] WITH CHECK ADD FOREIGN KEY([ID_MD])
REFERENCES [T_MD] ([ID_MD])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
The regex will send me the entire contents of this channel to be cut in two, stopping at the first "GO" ...
Re: Recovery of a string between two words
I have responded according to what was in the first message, I can not guess what you need. So you want to retrieve for each query "ALTER TABLE [NAME_OF_THE_TABLE]" to "GO"?
Here is one that can do this:
Code:
Regex taregex = new Regex(@"(?<req>ALTER TABLE \[(?<table>[\w]*)\](.+\n)*\bGO\b)" );
In Groups ["table"], you retrieve the table name and Groups ["req"], complete the request.
Multiple queries to retrieve, and use a MatchCollection course the game with a foreach, for example.
Re: Recovery of a string between two words
Indeed, I had not said. Finally I did not think that this could happen. That is how I do:
Code:
string chain = @"ALTER TABLE [T_AA] WITH CHECK ADD FOREIGN KEY([ID_BB])
REFERENCES [T_BB] ([ID_BB])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [T_FA] WITH CHECK ADD FOREIGN KEY([ID_MD])
REFERENCES [T_MD] ([ID_MD])
ON UPDATE CASCADE
ON DELETE CASCADE
GO";
Regex maregex = new Regex(@"(?<req>ALTER TABLE \[(?<table>[\w]*)\](.+\n)*\bGO\b)" );
MatchCollection matches = maregex.Matches(chain);
Match premiermatch = matches[0];
Match secondmatch = matches[1];
when I run the code, permiermatch still contains all the ALTER TABLE. It starts from first to last ALTER TABLE GO. So the line in secondmatch by exception.
Re: Recovery of a string between two words
OK so there is no blank line between "GO" and "ALTER TABLE" then? It is normal that it does not then since I had the regex based on the fact that GO ALTER TABLE and were separated by a blank line (the famous (.+\N)*).
It is possible to have more details on the ALTER TABLE? They are always the same? :
ALTER TABLE [XXXX] WITH CHECK ADD FOREIGN KEY ([XXXX])
REFERENCES [XXXX] ([XXXX])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
Or otherwise they have always 5 lines? Basically what are their common put a part "ALTER TABLE" and "GO"