1

May I use a RegEx (insted of Substring) in order to get a string in a string?

I'd like to get just the table names from a series of INSERT statements

INSERT INTO tableA VALUES (col1, col2, col3);
INSERT INTO tableB VALUES (col1, col2, col3);
INSERT INTO tableC VALUES (col1, col2, col3);

Using a regEx I would like to get (single line as I'm reading from a file):

tableA
tableB
tableC

I've tried with this expression (INTO )([a-z_])* which gives me 'INTO tableA' which I can use a SubString or Replace to give me the rest, but I'm guessing this may be done in RegEx.

1
  • What language are you using? Regex capabilities vary a lot from langauge to language. Commented Jan 12, 2012 at 10:34

5 Answers 5

2

Use this regex with lookbehind:

(?i)(?<=into\s+)\S+

var tables = Regex.Matches(s, @"(?i)(?<=into\s+)\S+")
    .Cast<Match>().Select(m => m.Value);
Sign up to request clarification or add additional context in comments.

Comments

1

Since you are using C#, I will specify how I would do it from start to finish:

        //create regex - the (.*?) is a capture group
        var regex = new Regex("INSERT INTO (.*?) VALUES");

        //mimic text lines read from a file
        var sqlStrings = new string[] {"INSERT INTO tableA VALUES (col1, col2, col3)", "INSERT INTO tableB VALUES (col1, col2, col3)", "INSERT INTO tableC VALUES (col1, col2, col3)"};
        foreach (var line in sqlStrings)
        {
            //get the first match with the regex we created
            var match = regex.Match(line);

            //print out the first capture group
            Console.WriteLine(match.Groups[1].ToString());
        }

This will write out the following:

tableA
tableB
tableC

Not sure of your exact input format (newlines or not) and exactly how you want to output it, but I hope this helps.

And yes, this can be done a lot more concise, but for clarity's sake I split it up over multiple lines and methods.

Comments

0

Use a text editor and Find + Replace as follows:

Find: ^INSERT INTO (.*) VALUES.*
Replace: \1

Be sure to check the Regular Expression option.

This is how my Notepad++ screen looks like and trust me, it has worked.

enter image description here

Comments

0

You can capture a substring from the matched string using parantheses:

^ *INSERT\s+INTO\s+(\w+)

From the match results, you can extract first captured group using \1 or $1 depending on your language.

* and \s+ are to ignore extra whitespaces.

Comments

0

in php

$regex = "/INSERT INTO (.*) VALUES/";

in java

String regex = "INSERT INTO (.*?) VALUES";

first capture group will hold what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.