Testing a simple select query using a regular expression

What is the regex pattern below the string (SQL query)?

Select col1,colo2,col3 form tablename where col1='this is a test'

No need to love the order different, etc.
A simple select and where clause.

0
source share
2 answers

This maintains a flexible distance between commas, etc. The raw regular expression should look like this:

^Select\s+(?:\w+\s*(?:(?=from\b)|,\s*))+from\s+\w+\s+where\s+\w+\s*=\s*'[^']*'$

You will need to avoid any platform-specific characters (for example, in C #, it \sshould be \\s.) In C #, it will look like this:@"^Select\s+(?:\w+\s*(?:(?=from\b)|,\s*))+from\s+\w+\s+where\s+\w+\s*=\s*'[^']*'$"

Also, be sure to make your expression ignore the case. In the end it can javascript on /iin the following way: /select ...+/i.

+4
source

, . - :

/select ([a-z0-9_]+,?)+ from [a-z0-9_]+ where [a-z1-9_]+='[a-z0-9_ ]+'/ 

+1

All Articles