jmeter (XVI) Regular Expressions

jmeter comes postprocessor: extractor regular expression can be used to extract information in response to the interface, the interface transmission parameters with subsequent administration.

For example, to extract response results in the field of token and sex field (response reads:
"Token": "83EEAA887F1D2F1AA1CDA9E197810992", "sex": 0, "userName": "12548650"), the following settings extractor,
Regular Expression Extractor Description:
Apply to: the scope of application (usually select the default of Main sample only), even if there redirect, it is generally Extract Interface final that request.
To check the response field: the sample data source.
Body: Interface response body content, usually to extract data common http response results are checked this.
Header: All contents of the response header.
Request Headers: All contents of the request header.
  url: url Sample is to match, i.e. the first row in the result tree view url requested content is not included in the data request parameters (i.e. only matched Protocol (protocol) + host + path + querystring, such as: https : //www.baidu.com/index.php tn = monline_3_dg)?.
  Response code: http response code, such as 101,200,302,404,501 and the like.
  Response information: http response information corresponding to the response code, for example: OK, Found (HTTP / 1.1 200 Ok; HTTP / 1.1 302 Found).
Reference name: variable name when the rest of the quote, the name can only be a, quote method: $ {token}. Figure
正则表达式:数据提取器,一般简单的通用语法就是:左边界(.*?)右边界,左右边界就是为了能准确定位到想匹配的内容,如最上面图的"token":"(.*?)","sex":(.*?),"userName", 其中"token":"以及","sex": 和,"userName"这3个就是左右边界,(.*?) 是替换了想要提取的内容,里面的'?'为非贪婪匹配,(非贪婪模式就是说在遇到第一个右边界后就停止匹配,这样就可以精确拿到想要的内容)。建议均使用非贪婪匹配,除非特殊情况。
模板:对应正则表达式提取器类型,样式为:$n$。若模板为:$0$,则为整个表达式匹配到的内容,就是包括小括号内跟小括号外的内容,即("token":"83EEAA887F1D2F1AA1CDA9E197810992","sex":0,"userName")。若模板为:$1$,则对应正则表达式中的第一个(.*?)所匹配的内容,即(83EEAA887F1D2F1AA1CDA9E197810992) ,若模板为:$2$,则对应正则表达式中的第二个(.*?)所匹配的内容,即(0),若模板为$1$$2$,则把2个(.*?)所匹配的内容拼接起来,即(83EEAA887F1D2F1AA1CDA9E1978109920)。模板是可以自由组合的,后续案例中再介绍。
匹配数字:正则表达式匹配数据的最终结果可以看做一个数组,匹配数字即可看做是数组的第几个元素。当为 0 时,随机返回匹配的数据。当为 1 时,表示返回匹配结果数组的第一个元素。当为负数(-1,-2,-100都可以)时,表示返回全部元素,并且同时会返回一个元素总数的变量token_matchNr,在引用时:通过${token_1}的方式来取第1个匹配的内容,${token_2}来取第2个匹配的内容。
缺省值:匹配失败时的默认值。通常用于后续的逻辑判断,建议使用一些特殊含义的,比如0,NULL,ERROR等。
 
正则测试:
可以直接在察看结果树里选择Regexp正则测试模式来测试正则是否写的正确。
 
正则结果查看
如何查看提取到了想要的内容呢,这里就需要提到另外一个后置处理器:Debug PostProcessor
该元件就为调试所用,一般用于查看变量值,添加方法同正则表达式提取器。
 
*********************************************************************************************************************************************************************************************************************************************************
接上(一),这里列出一部分笔者在平时使用中所碰到的提取实例。
1.信息头内容提取
这里有个小知识点,右边界用了'\n',因为很多信息头都是会换行的,如果是要匹配整行的话就需要加上这个换行符。

2.请求头内容提取

类同响应头提取。

3.URL内容提取

URL提取只能提取url里的内容,包括请求域名、接口路径以及跟在url后面的querystring类型的参数,而不能提取在表单里提交的参数。

4.表达式里可以带变量

如下图u是一个变量,可以在表达式里调用这个变量再进行正则匹配。

5.模板拼接

如图中的模板$1$++$2$,把2个括号匹配到的内容再加上'++'拼接起来,就类似于字符串拼接。

6.提取所有结果

匹配数字输入负数(-1)以后,就可以提取出所有的结果,并且会返回一个匹配总数(matchNr),可以用来给后续的操作使用,比如循环次数控制就可以用到这个匹配总数。

Guess you like

Origin www.cnblogs.com/sunyale/p/11206986.html