如何将通过正则表达式提取的所有值写入文件? [英] How can I write all values extracted via regex to a file?

查看:72
本文介绍了如何将通过正则表达式提取的所有值写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一块正则表达式,已使用regexp测试器在JMeter中进行了测试,它会返回多个结果(10),这正是我所期望的.

I've got a piece of regex which I've tested in JMeter using the regexp tester and it returns multiple results (10), which is what I'm expecting.

我正在使用正则表达式提取器来检索值,我想将所有值都写入CSV文件.我正在使用Beanshell后处理器,但我只知道一种将1值写入文件的方法.

I'm using the Regular Expression Extractor to retrieve the values and I would like to write ALL of them to a CSV file. I'm using the Beanshell Post Processor but I am only aware of a method to write 1 value to file.

到目前为止,我在Beanshell中的脚本是

My script in Beanshell so far:

temp = vars.get("VALUES"); // VALUES is the Reference Name in regex extractor

FileWriter fstream = new FileWriter("c:\\downloads\\results.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(temp);
out.close();

如何将通过正则表达式找到的所有值写入文件?谢谢.

How can I write all the values found via the regex to file? Thanks.

推荐答案

如果您要研究

If you'll look into Debug Sampler output, you'll see that VALUES will be a prefix.

喜欢

  • VALUES = ...
  • VALUES_g = ...
  • VALUES_g0 = ...
  • VALUES_g1 = ...

您可以使用 ForEach控制器对其进行迭代.

You can use ForEach Controller to iterate over them.

如果您要继续使用 Beanshell -您将需要遍历所有变量,例如:

If you want to proceed with Beanshell - you'll need to iterate through all variables like:

    import java.io.FileOutputStream;
    import java.util.Map;
    import java.util.Set;

    FileOutputStream out = new FileOutputStream("c:\\downloads\\results.txt", true);
    String newline = System.getProperty("line.separator");
    Set variables = vars.entrySet();

    for (Map.Entry entry : variables) {
        if (entry.getKey().startsWith("VALUES")) {
            out.write(entry.getValue().toString().getBytes("UTF-8"));
            out.write(newline.getBytes("UTF-8"));
            out.flush();
        }
    }

    out.close();

这篇关于如何将通过正则表达式提取的所有值写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆