Table of contents
1. Jmeter regular expression extractor
Preface
The Jmeter regular expression extractor is a type of Jmeter post processors and is used to read the results requested by the sampler in the form of regular expressions.
1. Jmeter regular expression extractor
1. Function
The JSON extractor can only extract response content, while the regular expression extractor can extract response headers, request header values, and of course response content.
2. Detailed explanation of parameters
- Apply to: Application scope, just select the default main sample only
- Response fields to check: Range of fields to extract
- Body: response body, does not include response headers; most commonly used
- body(unescaped): response body, replacing all HTML escape characters; not recommended
- body as a Document: Extract text from different types of files; affects performance
- Information header: response header
- Requeste Headers: request header
- URL:URL
- Response code: Response code (Response code)
- Response message: Response message
- Reference name: the variable name that receives the extracted value, * must be passed
- Regular Expressions: regular expressions
- Template: A template that creates a string from the found matches
- If a regular expression has multiple extraction results, the extraction results are in the form of an array
- Template 1, 2... means assigning the parsed value to the variable, starting from 1.
- 0 means what the entire expression matches
- If there is only one result, it can only be 1
- Matching numbers (0 represents random): which value to take (0: random, default; -1 for all; 1 for the first value), not required
- Default value: Default value. This value is taken when no value is matched. It is not required.
- Use empty default value: After checking, if no value can be extracted, an empty string will be returned.
Name of created variable (store the value intercepted by the regular expression into a variable and specify the name of the variable).
When using a regular expression to intercept multiple fields from a request return information, you can also fill in only A variable, as shown below.
At this time, the multiple intercepted values will be used with variable name _g1 and variable name _g2. See the picture below for details. This is a screenshot of my own special experiment.
- Variable = all intercepted fields, separated by spaces, as shown in the first line of the above figure.
- Variable_g: should represent the number of intercepted variables?
- Variable _g0: The complete field matched according to the regular expression (before interception).
- Variable_g1: The first field intercepted, when used is ${variable name_g1}.
- Variable_g2: The second field intercepted, when using ${variable name_g2}, as shown in the figure below.
Regular Expression
You can use Baidu 在线正则表达式
to practice regular expressions. Here is an online regular expression URL .
Template ($1$ means that you will get the data matched by the first regular expression, to be confirmed). According to
my own understanding, the meaning of template Template is the return format template of the extraction result (when you set it in Regular Expression) When there are multiple extraction points, such as "errmsg":"(.*)", "access_token":"(.+?)", the multiple extracted values will be returned to the user in the format set by the template).
The information I currently know from other online business blogs is that if multiple extraction points are filled in at the regular expression of the extractor (for example, "errmsg":"(.*)","access_token":"(.+? )". For Jmeter, this expression will be treated as two regular expressions - "errmsg":"(.*)","access_token": and,"access_token":"(.+ ?)", you can fill in $1$, $2$, etc. in the Template. $1$ represents the interception result of the first regular expression "errmsg":"(.*)","access_token":, $2$ represents the interception result of the first regular expression "errmsg":"(.*)","access_token": The interception results of the two regular expressions, "access_token": "(.+?)", and so on (Note: If there are spaces between $1$ and $2$ filled in at the Template, the extracted (.*) and ( .+?) will also be separated by corresponding spaces).
$0$ refers to full-text matching, $1$ $2$ refers to obtaining the first and second extraction points at the same time.
Match No. (successful match)
Default Value (when no match is found, use this value to return)
The parameters are hard to understand, so let’s give an example~
3. Regular expression extractor implementation
Or use the interface of this address to test
YesApi free interface for beginners | Guochuang Cloud, one account, handles interface development
2. Getting Started Example
The response content of the member login interface:
{
"ret": 200,
"data": {
"err_code": 0,
"err_msg": "",
"uuid": "F118DD335346CE9D7094C97190587108",
"token": "C1CA708E57F0C33E548745B0D82ADA2A800D886D17BA44261459C39C52376C0D",
"role": "user"
},
"msg": "V3.3.0 YesApi App.User.Login 2022-03-20 10:25:40 hd215"
}
Regular expression extractor - extract token:
Regex Extractor - Extract uuid:
- Regular expression-----
( )
Write matching rules inside, used to parse regular expressions - Regular expression-----
.*?
means to match any character of any length. This is also the most commonly used regular expression. - Generally (.+?) and (.*?) can meet 80% of our usage scenarios
Other interfaces call variables extracted by regular expressions:
3. Advanced examples
Test interface: http://api.yesapi.cn/docs-api-App.User.GetList.html
Response content:
{
"ret":200,
"data":
{
"err_code":0,
"err_msg":"",
"users":
[
{
"uuid":"F118DD335346CE9D7094C97190587108",
"username":"xey032001",
"role":"user",
"role_desc":"普通会员",
"status":0,
"status_desc":"正常",
"reg_time":"2022-03-20 10:25:40",
"ext_info":
{
"yesapi_avatar":"",
"yesapi_nickname":"",
"yesapi_email":"",
"yesapi_sex":"",
"yesapi_real_name":"",
"yesapi_points":0,
"yesapi_reg_source":"",
"yesapi_reg_note":"",
"yesapi_mobile":"",
"yesapi_expiration_time":""
}
},
{
"uuid":"AAC71046CFA51B8A55D4367652791D66",
"username":"xey04",
"role":"user",
"role_desc":"普通会员",
"status":0,
"status_desc":"正常",
"reg_time":"2022-03-19 13:22:24",
"ext_info":
{
"yesapi_avatar":"",
"yesapi_nickname":"",
"yesapi_email":"",
"yesapi_sex":"",
"yesapi_real_name":"",
"yesapi_points":0,
"yesapi_reg_source":"",
"yesapi_reg_note":"",
"yesapi_mobile":"",
"yesapi_expiration_time":""
}
},
{
"uuid":"97F22D1CD99D17C2CF29510C728F6570",
"username":"xey03",
"role":"user",
"role_desc":"普通会员",
"status":0,
"status_desc":"正常",
"reg_time":"2022-03-19 13:18:56",
"ext_info":
{
"yesapi_avatar":"",
"yesapi_nickname":"",
"yesapi_email":"",
"yesapi_sex":"",
"yesapi_real_name":"",
"yesapi_points":0,
"yesapi_reg_source":"",
"yesapi_reg_note":"",
"yesapi_mobile":"",
"yesapi_expiration_time":""
}
},
{
"uuid":"770A6910AF264AEB73F6ECAFEB5BBFB6",
"username":"xey02",
"role":"user",
"role_desc":"普通会员",
"status":0,
"status_desc":"正常",
"reg_time":"2022-03-19 13:15:35",
"ext_info":
{
"yesapi_avatar":"",
"yesapi_nickname":"",
"yesapi_email":"",
"yesapi_sex":"",
"yesapi_real_name":"",
"yesapi_points":0,
"yesapi_reg_source":"",
"yesapi_reg_note":"",
"yesapi_mobile":"",
"yesapi_expiration_time":""
}
},
{
"uuid":"690815A62298F01009010BA062773A9F",
"username":"xey01",
"role":"user",
"role_desc":"普通会员",
"status":0,
"status_desc":"正常",
"reg_time":"2022-03-19 13:14:03",
"ext_info":
{
"yesapi_avatar":"",
"yesapi_nickname":"",
"yesapi_email":"",
"yesapi_sex":"",
"yesapi_real_name":"",
"yesapi_points":0,
"yesapi_reg_source":"",
"yesapi_reg_note":"",
"yesapi_mobile":"",
"yesapi_expiration_time":""
}
},
{
"uuid":"24249E71ABBD72D7C1DE8B3E95A0FBBE",
"username":"xey",
"role":"user",
"role_desc":"普通会员",
"status":0,
"status_desc":"正常",
"reg_time":"2022-03-19 13:08:25",
"ext_info":
{
"yesapi_avatar":"",
"yesapi_nickname":"",
"yesapi_email":"",
"yesapi_sex":"",
"yesapi_real_name":"",
"yesapi_points":0,
"yesapi_reg_source":"",
"yesapi_reg_note":"",
"yesapi_mobile":"",
"yesapi_expiration_time":""
}
}
],
"total":6
},
"msg":"V3.3.0 YesApi App.User.GetList 2022-03-20 11:16:58 hd215"
}
1) Extract a value:
Only one value is extracted from the regular expression, and the matching number is not -1
1. The template is not filled in
PS: Fill in 1 for the matching number, which means that if there are multiple uuids, the first one will be matched. This is the same as the JSON extractor.
Test Results:
uuid=
uuid_g=1
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108"
uuid_g1=F118DD335346CE9D7094C97190587108
As you can see, if the regular expression matches the value but the template is not filled in, empty will be returned.
2. Write $0$ in the template
Test Results:
uuid="uuid":"F118DD335346CE9D7094C97190587108"
uuid_g=1
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108"
uuid_g1=F118DD335346CE9D7094C97190587108
You can see that if the template is filled in with $0$, the uuid is uuid_g0
3. Write $1$ in the template
Test Results:
uuid=F118DD335346CE9D7094C97190587108
uuid_g=1
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108"
uuid_g1=F118DD335346CE9D7094C97190587108
You can see that if the template writes $1$, the uuid is uuid_g1
4. Write $2$ in the template (does not exist)
Test Results:
uuid=null
uuid_g=1
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108"
uuid_g1=F118DD335346CE9D7094C97190587108
As you can see, if the template does not exist, the uuid is null (null is returned even if Use empty default value is checked)
2) Extract multiple values:
Test Results:
uuid_1=F118DD335346CE9D7094C97190587108
uuid_1_g=1
uuid_1_g0="uuid":"F118DD335346CE9D7094C97190587108"
uuid_1_g1=F118DD335346CE9D7094C97190587108
uuid_2=AAC71046CFA51B8A55D4367652791D66
uuid_2_g=1
uuid_2_g0="uuid":"AAC71046CFA51B8A55D4367652791D66"
uuid_2_g1=AAC71046CFA51B8A55D4367652791D66
uuid_3=97F22D1CD99D17C2CF29510C728F6570
uuid_3_g=1
uuid_3_g0="uuid":"97F22D1CD99D17C2CF29510C728F6570"
uuid_3_g1=97F22D1CD99D17C2CF29510C728F6570
uuid_4=770A6910AF264AEB73F6ECAFEB5BBFB6
uuid_4_g=1
uuid_4_g0="uuid":"770A6910AF264AEB73F6ECAFEB5BBFB6"
uuid_4_g1=770A6910AF264AEB73F6ECAFEB5BBFB6
uuid_5=690815A62298F01009010BA062773A9F
uuid_5_g=1
uuid_5_g0="uuid":"690815A62298F01009010BA062773A9F"
uuid_5_g1=690815A62298F01009010BA062773A9F
uuid_6=24249E71ABBD72D7C1DE8B3E95A0FBBE
uuid_6_g=1
uuid_6_g0="uuid":"24249E71ABBD72D7C1DE8B3E95A0FBBE"
uuid_6_g1=24249E71ABBD72D7C1DE8B3E95A0FBBE
uuid_matchNr=6
A regular expression has multiple ( ):
1. The template is empty:
Test Results:
uuid=
uuid_g=2
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108","username":"xey032001"
uuid_g1=F118DD335346CE9D7094C97190587108
uuid_g2=xey032001
You can see that if the template is empty, the value cannot be obtained and the uuid is empty.
2. Only one template:
Test Results:
uuid=F118DD335346CE9D7094C97190587108
uuid_g=2
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108",
"username":"xey032001"
uuid_g1=F118DD335346CE9D7094C97190587108
uuid_g2=xey032001
You can see that if there is only one template, uuid_g1 is obtained
3. There are two templates:
Test Results:
uuid=F118DD335346CE9D7094C971q90587108,
xey032001
uuid_g=2
uuid_g0="uuid":"F118DD335346CE9D7094C97190587108",
"username":"xey032001"
uuid_g1=F118DD335346CE9D7094C97190587108
uuid_g2=xey032001
As you can see, uuid takes two values uuid_g1 and uuid_g2, and they are spliced with commas
Notice:
- You only need to fill in one reference name, matching number, and default value, and do not need to match the number of regular expressions.
- You can also use - splicing, as long as the template also uses. or - splicing, for example, fill in the template: 1-2q
Reference article: