Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Regex to extract table names from a file containing SQL queries

Tags:

c#

regex

I've a text file containing large number of queries. I want to get all the distinct tables used in the entire file in all the queries. The table name can come after a 'from' or 'join'. How can i extract them by doing a regex match. Can anyone suggest a regular expression to get the matches?

like image 285
NLV Avatar asked Nov 18 '10 11:11

NLV


People also ask

Can you use RegEx with SQL?

You can use RegEx in many languages like PHP, Python, and also SQL. RegEx lets you match patterns by character class (like all letters, or just vowels, or all digits), between alternatives, and other really flexible options.

Why RegEx is used in SQL?

REGEXP is the operator used when performing regular expression pattern matches. RLIKE is the synonym. It also supports a number of metacharacters which allow more flexibility and control when performing pattern matching. The backslash is used as an escape character.

What is RegEx query?

Regexp queryedit. Returns documents that contain terms matching a regular expression. A regular expression is a way to match patterns in data using placeholder characters, called operators. For a list of operators supported by the regexp query, see Regular expression syntax.


1 Answers

It depends on structure of your file. Try to use this:

(?<=from|join)(\s+\w+\b)

Also turn on options Multiline if your not split your file in array or smth else with singleline string members. Also try to turn on IgnorCase option.

like image 80
arena-ru Avatar answered Nov 14 '22 21:11

arena-ru