首页 > 解决方案 > 我需要 org.sqlite.SQLiteException 的正则表达式:[SQLITE_CONSTRAINT_UNIQUE] A UNIQUE 约束失败

问题描述

我只想在“org.sqlite.SQLiteException: [SQLITE_CONSTRAINT_UNIQUE] A UNIQUE constraint failed (UNIQUE constraint failed: Items.Name)”中打印“A UNIQUE constraint failed”这条消息作为输出正则表达式的输入必需的。

标签: javaregex

解决方案


这只是匹配和之间的字符串](并且应该是一个开始。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "\\] (.*) \\(";
final String string = "org.sqlite.SQLiteException: [SQLITE_CONSTRAINT_UNIQUE] A UNIQUE constraint failed (UNIQUE constraint failed: Items.Name)";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}

推荐阅读