Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 16-04-2009
Member
 
Join Date: Nov 2008
Posts: 1,054
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?
Reply With Quote
  #2  
Old 16-04-2009
Member
 
Join Date: May 2008
Posts: 685
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);
Reply With Quote
  #3  
Old 16-04-2009
Member
 
Join Date: Nov 2008
Posts: 1,054
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" ...
Reply With Quote
  #4  
Old 16-04-2009
Member
 
Join Date: May 2008
Posts: 685
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.
Reply With Quote
  #5  
Old 16-04-2009
Member
 
Join Date: Nov 2008
Posts: 1,054
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.
Reply With Quote
  #6  
Old 16-04-2009
Member
 
Join Date: May 2008
Posts: 685
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"
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Recovery of a string between two words"
Thread Thread Starter Forum Replies Last Post
How to create recovery partition with recovery image in vista to restore the system Myra Vista Setup and Install 7 13-08-2010 10:39 AM
Breaking a String into Words In Java Bhardwaj Software Development 5 16-02-2010 05:21 PM
Is anybody how to count number of words in MS Words? Sonam Goenka Windows Software 5 21-01-2010 02:49 PM
Search the presence of n words in a string cobra2008 Software Development 3 09-10-2009 09:50 PM
How to Manipulate String using PHP String Functions ComPaCt Software Development 3 21-09-2009 09:07 AM


All times are GMT +5.5. The time now is 05:54 AM.