首页 文章

如何将通过正则表达式提取的所有值写入文件?

提问于
浏览
0

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

我正在使用正则表达式提取器来检索值,我想将它们全部写入CSV文件 . 我正在使用Beanshell Post Processor,但我只知道将1个值写入文件的方法 .

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

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();

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

2 回答

  • 2

    如果您查看Debug Sampler输出,您会看到 VALUES 将是一个前缀 .

    喜欢

    • VALUES = ...

    • VALUES_g = ...

    • VALUES_g0 = ...

    • VALUES_g1 = ...

    等等

    您可以使用ForEach Controller迭代它们 .

    如果你想继续Beanshell - 你需要遍历所有变量,如:

    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();
    
  • 3

    要将values数组的内容写入文件,以下代码应该可以工作(未经测试):

    String[] values = vars.get("VALUES");
    
    FileWriter fstream = new FileWriter("c:\\downloads\\results.txt", true);
    BufferedWriter out = new BufferedWriter(fstream);
    
    for(int i = 0; i < values.length; i++)
    {
       out.write(values[i]);
       out.newLine();
       out.flush();
    }
    out.close();
    

相关问题