List<MatchResult> collectAllMatches = new ArrayList<MatchResult>();
try {
PatternMatcher matcher = JMeterUtils.getMatcher();
PatternMatcherInput input = new PatternMatcherInput(textToMatch);
while (matcher.contains(input, searchPattern)) {
MatchResult match = matcher.getMatch();
collectAllMatches.add(match);
}
} finally {
if (name.length() > 0){
vars.put(name + "_matchNr", Integer.toString(collectAllMatches.size())); //$NON-NLS-1$
}
}
if (collectAllMatches.size() == 0) {
return defaultValue;
}
if (valueIndex.equals(ALL)) {
StringBuilder value = new StringBuilder();
Iterator<MatchResult> it = collectAllMatches.iterator();
boolean first = true;
while (it.hasNext()) {
if (!first) {
value.append(between);
} else {
first = false;
}
value.append(generateResult(it.next(), name, tmplt, vars));
}
return value.toString();
} else if (valueIndex.equals(RAND)) {
MatchResult result = collectAllMatches.get(rand.nextInt(collectAllMatches.size()));
return generateResult(result, name, tmplt, vars);
} else {
try {
int index = Integer.parseInt(valueIndex) - 1;
MatchResult result = collectAllMatches.get(index);
return generateResult(result, name, tmplt, vars);
} catch (NumberFormatException e) {
float ratio = Float.parseFloat(valueIndex);
MatchResult result = collectAllMatches
.get((int) (collectAllMatches.size() * ratio + .5) - 1);
return generateResult(result, name, tmplt, vars);
} catch (IndexOutOfBoundsException e) {
return defaultValue;
}