Dynamic web responses sometimes place request values in text that is not clean JSON, XML, or selector-friendly HTML. The JMeter Regular Expression Extractor captures a matched group after a sampler runs, so a later sampler can reuse a token, ID, header value, or short text fragment from the same virtual user flow.

An extractor runs after its parent sampler and stores its result in a variable belonging to the current thread. Attaching it directly to the request that receives the value keeps unrelated responses from overwriting that variable before the next request uses it.

A regular expression depends on the response's spelling, quotes, and field order. Structured HTML and JSON are usually easier to maintain with a CSS Selector Extractor or JSON Extractor; regex is useful for mixed text, headers, or a small predictable fragment. A one-user run exposes a missed match before a larger test repeats the same failure.

The starting plan contains the HTTP request that receives the value, the later request that submits it, and a View Results Tree listener. Both requests run in that order in the same Thread Group. The checkout form shown here returns a hidden field named csrf_token.

Steps to extract values with a JMeter Regular Expression Extractor:

  1. Check that the validation Thread Group has one user and one loop.

    A checkout request can create a real order. The validation plan should target a test environment with disposable data.

  2. Run the plan from Run → Start to capture the form response.
  3. Inspect the form request's Response data → Response Body in View Results Tree for the value to capture.

    The hidden input has name="csrf_token" followed by its value attribute. The HTML Source Formatted renderer makes the markup easier to read; the extractor will match the original response body.

  4. Add Regular Expression Extractor beneath the form request through its right-click Add → Post Processors menu.

    A child of the form request runs only after that request. An extractor beneath the Thread Group would also run after other samplers in its scope.

  5. Set Name of created variable to csrf_token.

    Main sample only and Body are already selected for this response. A value returned in a header needs Response Headers instead.

  6. Enter the capture pattern in Regular Expression.
    name="csrf_token"\s+value="([^"]+)"

    The parentheses capture the value without its quotes. This pattern depends on the two attributes appearing in this order; a different response needs a pattern that matches its actual text. Slash delimiters are not required.

  7. Set Template to $1$.

    $1$ stores the first parenthesized group. $0$ would store the whole match, including the field name and surrounding markup.

  8. Set Match No. (0 for Random) to 1.

    This selects the first occurrence. A value of 0 chooses a random match; a negative value creates numbered variables for all matches.

  9. Set Default Value to TOKEN_NOT_FOUND.

    The visible fallback distinguishes a missed match from an empty value. The Use empty default value option remains unchecked.

  10. Set the later request's csrf_token parameter value to ${csrf_token}.
    Name: csrf_token
    Value: ${csrf_token}

    The parameter name belongs to the application; the variable name inside ${...} belongs to JMeter. They happen to be the same here.

  11. Save the test plan with Ctrl+S.
  12. Clear the old samples with Run → Clear All.
  13. Run the updated plan from Run → Start.
  14. Confirm that the later request's Request → Request Body contains the token from the latest form response.

    The form and submitted parameter show the same value. TOKEN_NOT_FOUND means the pattern did not match; a literal ${csrf_token} usually means the extractor did not run or the variable name differs. A Debug Sampler can also expose the variable directly.
    Related: How to add a Debug Sampler in JMeter

  15. Check that the later request's Response data reports a successful submission.

    The checkout response says Checkout complete after accepting the token. A green sample or HTTP 200 alone may still contain an application error. Temporary debug samplers and View Results Tree are suitable for this small run; large load tests need fewer listeners.
    Related: How to disable heavy JMeter listeners before a load test
    Related: How to run a JMeter test from the command line

Share this guide