我正在寻找使用一行 String.split() 在我正在编写的查询中提取“名称”..
Pattern + Matcher 像预期的那样工作,但我正在努力弄清楚为什么 String.split() 不返回匹配项!
public static void main(String[] asdf)
{
final String queryText = "id <equals> `1` <AND> name <equals> `some name`";
final String regex = "^(.*name <equals> `)([\\S\\s]*)(`.*)$";
System.out.println("Splitting...");
final String[] split = queryText.split(regex);
for (int i = 0; i < split.length; i++)
{
System.out.println(split[i]);
}
System.out.println("Matching...");
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(queryText);
if (matcher.find())
{
for (int i = 0; i < matcher.groupCount(); i++)
{
System.out.println(matcher.group(i + 1));
}
}
}
打印输出
Splitting...
Matching...
id <equals> `1` <AND> name <equals> `
some name
`