Skip to content
Permalink
Browse files
Add a switch to decide which regex engine should be used
  • Loading branch information
FSchumacher committed Mar 5, 2022
1 parent 2c6bdf4 commit eb7c88c6cc15c7eb3c476d1b2d2b420361db6128
Showing 1 changed file with 32 additions and 0 deletions.
@@ -18,6 +18,7 @@
package org.apache.jmeter.engine.util;

import java.util.Map;
import java.util.regex.PatternSyntaxException;

import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.testelement.property.JMeterProperty;
@@ -53,6 +54,8 @@ public class ReplaceFunctionsWithStrings extends AbstractTransformer {

private final boolean regexMatch;// Should we match using regexes?

private boolean useJavaRegex = JMeterUtils.getPropDefault("jmeter.use_java_regex", false);

public ReplaceFunctionsWithStrings(CompoundVariable masterFunction, Map<String, String> variables) {
this(masterFunction, variables, false);
}
@@ -66,6 +69,13 @@ public ReplaceFunctionsWithStrings(CompoundVariable masterFunction, Map<String,

@Override
public JMeterProperty transformValue(JMeterProperty prop) throws InvalidVariableException {
if (useJavaRegex) {
return transformValueWithJavaRegex(prop);
}
return transformValueWithOroRegex(prop);
}

private JMeterProperty transformValueWithOroRegex(JMeterProperty prop) throws InvalidVariableException {
PatternMatcher pm = JMeterUtils.getMatcher();
PatternCompiler compiler = new Perl5Compiler();
String input = prop.getStringValue();
@@ -91,6 +101,28 @@ public JMeterProperty transformValue(JMeterProperty prop) throws InvalidVariable
return new StringProperty(prop.getName(), input);
}

private JMeterProperty transformValueWithJavaRegex(JMeterProperty prop) throws InvalidVariableException {
String input = prop.getStringValue();
if(input == null) {
return prop;
}
for(Map.Entry<String, String> entry : getVariables().entrySet()){
String key = entry.getKey();
String value = entry.getValue();
if (regexMatch) {
try {
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(constructPattern(value));
input = pattern.matcher(input).replaceAll(FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX);
} catch (PatternSyntaxException e) {
log.warn("Malformed pattern: {}", value);
}
} else {
input = StringUtilities.substitute(input, value, FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX);
}
}
return new StringProperty(prop.getName(), input);
}

/**
* Normal regexes will be surrounded by boundary character matches to make life easier for users.
* If a user doesn't want that behaviour, they can prevent the modification by giving a regex, that

0 comments on commit eb7c88c

Please sign in to comment.