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)(?:UNIQUE\s+KEY|UNIQUE\s+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?
(?:KEY\s+`[^`]*`|(?:INDEX|UNIQUE\s+KEY)\s+\w+|UNIQUE\s+(?!KEY\s*\()\w+)\s*\(`[^`]*`\)`is tricky inside comments.