I am looking to use a one line String.split() to pull out the 'name' in a query I am writting..
The Pattern + Matcher works like expected, but I am pulling my hair out trying to figure out why String.split() doesn't return a match!
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));
}
}
}
Prints the output
Splitting...
Matching...
id <equals> `1` <AND> name <equals> `
some name
`
splitby the match, the two resulting strings are empty. And trailing empty strings are omitted by default. Use the [overload with an integer argument](docs.oracle.com/javase/1.4.2/docs/api/java/lang/…, int)) and supply a negative integer to get the empty strings, if that is what you want. In Java,splitdoes not return the matched groups in addition to the splits (as it works in some other languages).[\\S\\s]is insane - it means "any character that is a whitespace or not a whitespace. It's the same as..and forgot to put it back.