Results 1 to 6 of 6

Thread: Recovery of a string between two words

  1. #1
    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?

  2. #2
    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);

  3. #3
    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" ...

  4. #4
    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.

  5. #5
    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.

  6. #6
    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"

Similar Threads

  1. Replies: 7
    Last Post: 13-08-2010, 10:39 AM
  2. Breaking a String into Words In Java
    By Bhardwaj in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 05:21 PM
  3. Is anybody how to count number of words in MS Words?
    By Sonam Goenka in forum Windows Software
    Replies: 5
    Last Post: 21-01-2010, 02:49 PM
  4. Search the presence of n words in a string
    By cobra2008 in forum Software Development
    Replies: 3
    Last Post: 09-10-2009, 09:50 PM
  5. How to Manipulate String using PHP String Functions
    By ComPaCt in forum Software Development
    Replies: 3
    Last Post: 21-09-2009, 09:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,391,638.53734 seconds with 16 queries