I can’t see a reason why the Matcher would return a match on the pattern, but split will return a zero length array on the same regex pattern. It should return something — in this example I’m looking for a return of 2 separate strings containing “param/value”.
public class MyClass {
protected Pattern regEx = "(([a-z])+/{1}([a-z0-9])+/?)*";
public void someMethod() {
String qs = "param/value/param/value";
Matcher matcherParamsRegEx = this.regEx.matcher(qs);
if (matcherParamsRegEx.matches()) { // This finds a match.
String[] parameterValues = qs.split(this.regEx.pattern()); // No matches... zero length array.
}
}
}
The pattern can match the entire string.
split()doesn’t return the match, only what’s in between. Since the pattern matches the whole string that only leaves an empty string to return. I think you might be under a misconception as to whatsplit()does.For example:
will return an array of 4 elements: param, value, param, value.
Notice that what you search on (“/”) isn’t returned.
Your regex is somewhat over-complicated. For one thing you’re using
{1}, which is unnecessary. Second, when you do([a-z])+you will capture exactly one latter (the last one encountered. Compare that to([a-z]+), which will capture the entire match. Also, you don’t even need to capture for this. The pattern can be simplified to:Technically this:
is a compiler error, so what you actually ran versus what you posted could be anything.