gpt4 book ai didi

java - 尝试使用replaceFirst将值添加到查询字符串参数字段

转载 作者:行者123 更新时间:2023-12-02 05:39:37 24 4
gpt4 key购买 nike

我在 HTTP Sampler 上添加了 JSR223 预处理器,目的是能够选择查询字符串参数并向其添加新值。即:

之前设计师=17827&size=3350&color=1

所选参数 = 大小
要添加的新值 = 9999

之后设计师=17827&size=3350|9999&color=1

为了做到这一点,我开发了下面的 Groovy 代码:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

def listing_page_filter_name = "size";
def filter_values_value = "9999";
def listing_filters = "designer=17827&size=3350&color=1";

def prefix = listing_page_filter_name;
Pattern r = Pattern.compile(prefix + '=(.+?)&|' + prefix + '=(.+?)\\$');
String pattern = r.toString();
//Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(listing_filters);

log.info("FILTER: " + listing_page_filter_name);
log.info("FILTER VALUE: " + filter_values_value);

if(m.find())
{
log.info("Found value ------> " + m.group(1));
def regex_match = m.group(1);
def replacement = regex_match + "|" + filter_values_value;
log.info('Replacing it by: $1' + replacement);
String str = m.replaceFirst(Pattern.quote(pattern),Pattern.quote(replacement));
log.info(str);
//log.info(m.replaceFirst(/=(.+?)&|/,"lalalalalalala"));
}

我得到的输出是:

2019-05-15 11:28:02,765 INFO o.a.j.m.J.JSR223 PreProcessor: RANDOMLY CHOSEN FILTER: size
2019-05-15 11:28:02,765 INFO o.a.j.m.J.JSR223 PreProcessor: RANDOMLY CHOSEN FILTER VALUE: 9999
2019-05-15 11:28:02,765 INFO o.a.j.m.J.JSR223 PreProcessor: Found value ------> 3350
2019-05-15 11:28:02,766 INFO o.a.j.m.J.JSR223 PreProcessor: Replacing it by: $13350|9999
2019-05-15 11:28:02,766 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor
javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.util.regex.Matcher.replaceFirst() is applicable for argument types: (java.lang.String, java.lang.String) values: [\Qsize=(.+?)&|size=(.+?)\$\E, \Q3350|9999\E]
Possible solutions: replaceFirst(java.lang.String)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.16.jar:2.4.16]
at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.16.jar:2.4.16]
at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_211]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:935) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:537) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.1.1 r1855137]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]
Caused by: groovy.lang.MissingMethodException: No signature of method: java.util.regex.Matcher.replaceFirst() is applicable for argument types: (java.lang.String, java.lang.String) values: [\Qsize=(.+?)&|size=(.+?)\$\E, \Q3350|9999\E]
Possible solutions: replaceFirst(java.lang.String)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:71) ~[groovy-all-2.4.16.jar:2.4.16]
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:48) ~[groovy-all-2.4.16.jar:2.4.16]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136) ~[groovy-all-2.4.16.jar:2.4.16]
at Script36.run(Script36.groovy:23) ~[?:?]
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321) ~[groovy-all-2.4.16.jar:2.4.16]
... 9 more

有谁知道为什么吗?

提前致谢!

最佳答案

对我来说,不使用正则表达式更容易:

def q0 = "designer=17827&size=3350&color=1"
def q1 = q0.split('&').collect{
it.split('=').with{
if(it[0]=='size'){
it[1]=it[1] + '|6641'
}
return it.join('=')
}
}.join('&')
println q1

但对于您的情况,使用replaceAll或replaceFirst更简单:

def q0 = "designer=17827&size=3350&color=1"
def key='designer'
def append = '|5555'
def q1 = q0.replaceAll('('+key+'=)([^&]*)', '$1$2'+append)
println q1

关于java - 尝试使用replaceFirst将值添加到查询字符串参数字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56147581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com