最近在研究数据库迁移工具,阅读源码的过程中学习了一些正则表达式的特殊用法,于是记录一下。
@Test
public void test() {
String sql = "CREATE TABLE `test_table` (\n" +
" `id` int(11) NOT NULL AUTO_INCREMENT,\n" +
" `test_column` int(11) DEFAULT NULL COMMENT '测试字段备注',\n" +
" PRIMARY KEY (`id`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='测试表备注'";
String regex = "^\\s*create\\s+table\\s+(?<tableName>\\S+)?[^()]*\\(" +
"(?<columnSql>[\\s\\S]+)" +
"\\)[\\s]+" +
"(engine=(?<tableEngine>\\S+))?" +
"[\\s]*" +
"(default charset=(?<tableCharset>\\S+))?" +
"[\\s]*" +
"(collate=(?<tableCollate>\\S+))?" +
"[\\s]*" +
"(comment=(?<tableComment>\\S+))?" +
"$";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(sql);
if (matcher.find()) {
System.out.println("tableName:" + matcher.group("tableName"));
System.out.println("columnSql:" + matcher.group("columnSql"));
System.out.println("tableEngine:" + matcher.group("tableEngine"));
System.out.println("tableCharset:" + matcher.group("tableCharset"));
System.out.println("tableCollate:" + matcher.group("tableCollate"));
System.out.println("tableComment:" + matcher.group("tableComment"));
}
}
输出结果为
tableName:`test_table`
columnSql:
`id` int(11) NOT NULL AUTO_INCREMENT,
`test_column` int(11) DEFAULT NULL COMMENT '测试字段备注',
PRIMARY KEY (`id`)
tableEngine:InnoDB
tableCharset:utf8mb4
tableCollate:utf8mb4_unicode_ci
tableComment:'测试表备注'
搜索发现jdk7之后正则表达式就已经支持了这种写法,而安卓端也有第三方的类库Github地址在几年前提供了支持。
语法:
(?<foo>bar)
英文解释:
Define a named group named "foo" consisting of pattern bar. Matches within the group can be referenced in a replacement using the backreference $foo.
大概意思就是定义一个命名组foo,命名组的匹配规则是bar,这样做还可以使用$foo的形式引用,当然这里也适用其它的正则语法,比如适用: (?: