0

What will be the Regular expression to find PRIMARY KEY and UNIQUE KEY ::

Sample create query:

CREATE TABLE `empiccrdb` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `ccrId` INT(11) NOT NULL DEFAULT '0',
  `data` MEDIUMTEXT,
  `delflag` TINYINT(2) DEFAULT '0',
  `isEncrypted` TINYINT(2) DEFAULT '0',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `ccrId` (`ccrId`)
) ENGINE=INNODB AUTO_INCREMENT=829 DEFAULT CHARSET=latin1
4

1 回答 1

1

Strictly speaking, as the comments on your question suggest, it's often wisest to avoid String parsing / processing other code unless you absolutely have to.

It throws up complications in the build process and issues for future change control.

Perhaps you are better to talk to the database, determine the table structure and find the keys from there?

Having said that, here is a java regex that could help you:

    String sql = "... put it here ...";

    String regex = "(PRIMARY|UNIQUE) KEY[ `a-zA-z]+\\(`([a-zA-Z]+)`\\)";
    Matcher matcher = Pattern.compile(regex).matcher(sql);
    while (matcher.find()) {
        System.out.println(matcher.group(1) + ": " + matcher.group(2));
    }
于 2013-05-16T10:33:16.183 回答