Kman
Kman

Reputation: 5001

RegEx find string in string

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.

Upvotes: 1

Views: 583

Answers (5)

Anders Arpi
Anders Arpi

Reputation: 8397

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.

Upvotes: 1

davogotland
davogotland

Reputation: 2777

in php

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

in java

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

first capture group will hold what you want.

Upvotes: 0

Amarghosh
Amarghosh

Reputation: 59451

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.

Upvotes: 0

adarshr
adarshr

Reputation: 62583

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

Upvotes: 0

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Use this regex with lookbehind:

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

var tables = Regex.Matches(s, @"(?i)(?<=into\s+)\S+")
    .Cast<Match>().Select(m => m.Value);

Upvotes: 2

Related Questions