0

i am new on Regular Expression.

I will test a String, wheter it is valid with a regular expression.

MY String is following:

`Select * from xddf_db.yyy_tte where adress = '[PARAM]' and surename like '%[ABC]'` .... can several times on the end with and/or

It can begin als with Select name must not with *

`Select Name` .....

The important is that the bracket fields should begin and end with '[sdf]' or never with apostroph for exampe [sdf] that is correct too. But it is not allowed to begin with apostroph and not end. For example '[asd] That should wrong.

The underline charcters "_" can in it but not must.

It is correct between/ Or not with apostroph. The fields can have have one percent symbol(it is for Like function). For Example it is correct '%[asd]' or %[asd]& correct toobe . That is correct too '[asd]%' but that is wrong: '%%[sdđ]%' . Only one it is allowed. I hope you can help we with this hard expression.


Here is the list:

Allowed:

Select * from xddf_db.yyy_tte where adress = '[PARAM]' and surename like '%[ABC]'
Select * from xddf_db.yyy_tte where adress = '[PARAM]' and surename like '%[ABC]&'
Select * from xddf_db.yyy_tte where adress = '[PARAM]' and surename like '[ABC]&'
Select Name from xddfdb.yyy where adress = '[PARAM]' and surename_abc ='[ABC]'
Select Name from xddfdb.yyy where adress = '[PARAM]' and surename_abc =[ABC]
Select * from xddfdb.yyy_tte where adress = '[PARAM]' and surename_abc =[ABC]
Select * from xddfdb.yyy_tte where adress = '[PARAM]' and surename_abc =[ABC] or post_dd like '%[ABC]'
Select * from xddfdb.yyy_tte where adress = '[PARAM]' and surename_abc =[ABC] or post_dd < [ABC] and post_dd > [ABD]

WRONG

Select * from xddfdb.yyy_tte where adress = [PARAM]' and surename_abc = '[ABC]
Select * from xddf_db.yyy_tte where adress = '[PARAM]' and surename like '%[ABC]&&'
Select Name from xddf_db.yyy_tte where adress = '[PARAM]' and surename like '%&[ABC]&&'
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Freed2704
  • 1
  • 2
  • 2
    Sorry, but alone your question is really hard to read. I would suggest that you put your POSITIVE / NEGATIVE examples on single lines, and like one example for each of the conditions. Beyond that: not everything can or should be solved used regular expressions. – GhostCat Feb 03 '17 at 08:36
  • See http://stackoverflow.com/a/1600061/982149 I guess you are trying to validate for correct SQL syntax ... – Fildor Feb 03 '17 at 08:40
  • 2
    Possible duplicate of [Any Java libraries out there that validate SQL syntax?](http://stackoverflow.com/questions/141499/any-java-libraries-out-there-that-validate-sql-syntax) – Fildor Feb 03 '17 at 08:42
  • And I agree with Fildor: don't start to create your OWN SQL parser replacement. Better look for re-using an existing solution; instead of re-inventing your own (most likely **deficient**) wheel. – GhostCat Feb 03 '17 at 08:54
  • Something like `^(?!.*'[^'\s]+(?:\s|$))(?!.*(?:%%|&&))(?i)select.*from.*where.*$` if you persist on using regex for such validation of a SQL. – LukStorms Feb 03 '17 at 10:24

0 Answers0