dimanche 12 juin 2016

Regex for validating SQL

I am writing a regex to validate that a name should be given when defining INDEX, KEY and UNIQUE while creating a new table. Some of the valid syntaxes are

KEY `id` (`id`),     
INDEX name (`name`),    
UNIQUE idx (`id`),     
UNIQUE KEY name (`name`),

Invalid syntaxes are

UNIQUE KEY (`name`),   
INDEX (`designation`),     
UNIQUE (`status`),    

because there is no name provided while defining them.

I came up with a regex like this

(?i)(?:UNIQUEs+KEY|UNIQUEs+INDEX|KEY|INDEX|UNIQUE)s*`?w+`?s*(

But it matches

UNIQUE KEY (`name`),

also. Is there anyway I can improve the regex not to match the above string?

Aucun commentaire:

Enregistrer un commentaire