- Last Updated: [[2020-12-04]]
- [[JMeter Regular Expression Extractor]] [[Correlation of dynamic values]]
-
- Here's an example of how to use the regular expression extractor in JMeter to correlate a long string and get two variables out of it.
- # Raw data to be parsed
- ```javascript
{"Id":907,"ShortName":"ReportingHazardIncidentInvestigation","LongName":"Resident Investigation Incident"}
```
- Imagine that there are many more strings like this in the response, and we want to get a random one.
- # Put this in the regular expression extractor
- ```javascript
Reference name: `formcreated`
Regular expression: `"Id":([0-9]+),"ShortName":"([^"]+)","LongName":"([^"]+)"`
Template: `$1$2$3
Match No. (0 for Random): 0
Default value: NOT_FOUND```
- Using a default value of `NOT_FOUND` or something similarly unique will give you a clue that there was something wrong with the extractor. In contrast, if you don't set a default value, you won't know whether the script has simply not executed the extractor yet or if the extractor failed.
- This takes the long string, and saves the groups (those delineated by parentheses) into variables).
- # Variables created by the extractor
- To be able to see what variables are created by the extractor, use the [[JMeter/Debug Post Processor]]. Here's what was created by the extractor above.
- ```javascript
formcreated=907ReportingHazardIncidentInvestigationResident Investigation Incident
formcreated_g=3
formcreated_g0="Id":907,"ShortName":"ReportingHazardIncidentInvestigation","LongName":"Resident Investigation Incident"
formcreated_g1=907
formcreated_g2=ReportingHazardIncidentInvestigation
formcreated_g3=Resident Investigation Incident```
- `formcreated` is the entire string, without the selectors having been removed.
- The `_g` refers to the number of groups that have been extracted. Each group corresponds to the selectors in the regular expression. (__Note: It's zero-indexed, so 3 actually means 4.__)
- # Using the extractor variables
- ## In a POST request
- ```javascript
{"Title":"${formcreated_g2}","DateStart":"${oneyearinfuture}","DateEnd":"Invalid date","TimeDue":"1800","LeadTime":0,"TaskNotes":"testing","CeaseRecurrence":1,"Frequency":0,"ScheduleDefinitionType":2,"FacilityId":${facilityid},"FormId":${formcreated_g1},"ResidentIds":[]}
```
-