One way to use the Java split function to not match the longest pattern is by utilizing the non-greedy quantifier ".?" instead of the greedy quantifier ".".
For example, if you want to split a string by a comma followed by any number of spaces, but you want to avoid matching the longest sequence of spaces that ends with a comma, you can use the following code:
String input = "apple, banana, pear, ,, orange";
String[] parts = input.split(",\\s*?(?=\\S)");
System.out.println(Arrays.toString(parts));
Output:
[apple, banana, pear, ,, orange]
In this code, we are using the split function with a regular expression pattern that matches a comma followed by any number of spaces using the ",\\s*?"
part. However, we are also using a lookahead pattern "(?=\\S)"
that checks if the next character after the spaces is not a whitespace character. This ensures that we split the string at the first comma followed by spaces that are not followed by more spaces and a comma, thereby avoiding the longest pattern. The non-greedy quantifier *?
ensures that the regex engine matches as few spaces as possible before the lookahead pattern.
Asked: 2023-07-09 15:41:28 +0000
Seen: 9 times
Last updated: Jul 09 '23