我想使用一行String.split()从我正在编写的查询中提取出“name”。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
`
[\\S\\s]
太疯狂了 - 它的意思是“任何空格或非空格字符”。它和.
一样。 - Bohemian