2

我有以下代码行:

Matcher matcher = Pattern.compile("CREATE TABLE ([^ ]*) \\(").matcher("CREATE TABLE DeliveryPointAddress (");

结果Matcher不包含匹配项,或更重要的是,不包含表名。

我需要改变什么才能"DeliveryPointAddress"在第 1 组中进行比赛?

4

1 回答 1

5

我不是通灵者,但我相信你的问题是你没有find()在查询匹配器之前打电话:

Matcher matcher = Pattern.compile("CREATE TABLE ([^ ]*) \\(").matcher(
                "CREATE TABLE DeliveryPointAddress (");
matcher.find();
System.out.println(matcher.group(1));
交货点地址

请注意,它find()返回一个布尔值,指示是否找到匹配项,因此通常您会按照以下方式执行操作

if (matcher.find()) {
    System.out.println(matcher.group(1));
}

但想法是一样的:find()先打电话!

于 2013-09-28T00:59:24.930 回答