0

Hey all I am wondering if anyone has a REGEX that can take a query like this:

SELECT pillers, Balloons, Tacks FROM the_database_file WHERE Balloons != 'small' AND Balloons != 'large'

And have it formatted like this:

SELECT pillers, Balloons, Tacks 
FROM the_database_file 
WHERE Balloons != 'small' 
AND Balloons != 'large'

I've been looking around and all I can find is finding an GO or something like that to start the split which wouldn't help me at all. Also, the select statement can have OR or more AND's in it as well. The above is just an example of one such query I would be running.

1
  • replace(/(?!^)(select|from|where|and|or)/ig, "\n$1") Commented Feb 17, 2016 at 18:35

2 Answers 2

1

Can you please state what is your purpose to format SQL query? There are so many online as well as offline tools available to format SQL query. So I am wondering why you want to do it using Regex :)

Like

Sign up to request clarification or add additional context in comments.

Comments

1

You can create a regex expression to captures the SELECT, FROM and WHERE parts into groups and use Regex.Replace() method to creates a new string like this one:

SELECT pillers, Balloons, Tacks 
FROM the_database_file 
WHERE Balloons != 'small' AND Balloons != 'large'

This is the code:

var sql = "SELECT pillers, Balloons, Tacks FROM the_database_file WHERE Balloons != 'small' AND Balloons != 'large'";
string pattern = "^(?<select>SELECT\\s+[\\w\\*\\.]+(?:[\\s\\,]+[\\w\\*\\.]+)*)\\s+(?<from>FROM\\s+\\w+)\\s+(?<where>(WHERE\\s+.+?)(\\s+(?:AND|OR)\\s+.+?)*)$";            
var result = Regex.Replace(sql, pattern, "${select}\n${from}\n${where}");

I think you can't create a line for each condition because you have no way to get all matches of the same capturing group.

But you can do something like this:

var sql = "SELECT pillers, Balloons, Tacks FROM the_database_file WHERE Balloons != 'small' AND Balloons != 'large'";

string pattern = "^(?<select>SELECT\\s+[\\w\\*\\.]+(?:[\\s\\,]+[\\w\\*\\.]+)*)\\s+(?<from>FROM\\s+\\w+)\\s+(?<where>(WHERE\\s+.+?)(\\s+(?:AND|OR)\\s+.+?)*)$";

Regex regex = new Regex(pattern);
var match = regex.Match(sql);

var all = match.Groups[0];
var select = match.Groups["select"];
var from = match.Groups["from"];
var where = match.Groups["where"];

var conditions = match.Groups.Cast<Group>().Except(new Group[] { all, select, from, where });

string result = string.Format("{0}\n{1}\n{2}", select.Value, from.Value, string.Join("\n", conditions.Select(c => c.Value.Trim())));

Console.WriteLine(result);

You can exclude from the groups the first one, the SELECT, the FROM and the WHERE groups, and the remaining groups will be the query conditions.

And this is what you get:

SELECT pillers, Balloons, Tacks
FROM the_database_file
WHERE Balloons != 'small'
AND Balloons != 'large'

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.