JMeter: How to use Regular Expression to extract the value of a duplicate field?

后端 未结 7 694
名媛妹妹
名媛妹妹 2021-01-17 02:35

I have the following Response Body JSON:

{
    \"address\": [
    {
        \"id\": \"1234\"
    }
    ],
    \"id\": \"d1a4f010-48d9-434b-9b3a-2d2b12f5e38c\         


        
相关标签:
7条回答
  • 2021-01-17 03:10

    I don't know jmeter but to get the value of second id this expression

     "id"\s*:.+?"id"\s*:\s*"([^"]*)" 
    

    It will return what you want on the $1 variable in a some languages. I suppose in jmeter you can have something similar to this in order to get the first group.

    Maybe ${MYREF_g1} according this page ?

    0 讨论(0)
  • 2021-01-17 03:11

    try with below expression

    "id": "([0-9a-z-])*+"
    
    0 讨论(0)
  • 2021-01-17 03:16

    Using JMeter, you can use Regular Expression Extractor ...

    Reference Name: myid
    Regular Expression: "id": "(.+?)"
    Template: $1$
    Match No.: 2
    

    If you specify using a Match No:...

    0 = Random Match
    1 = First Match 
    2 = Second Match
    etc....
    

    Or use corresponding variable to access the match. ${myid_2}

    The variables are set as follows:

    myid_matchNr - Number of matches found, possibly 0
    myid_n       - (n = 1, 2, etc..) Generated by the template
    myid_n_gm    - (m = 0, 1, 2) Groups for the match (n) 
    myid         - By itself it is always set to the default value
    myid_gn      - Not set at all
    

    Or judging by this case, if you prefer just regex and your strings are exactly as stated. You could do..

    ],\s+"id": "(.+?)"
    
    0 讨论(0)
  • 2021-01-17 03:21

    I tried with this:

    "id":\s*"([0-9a-f\-]*)"
    
    0 讨论(0)
  • 2021-01-17 03:22

    The given response values

    {
        "address": [
        {
            "id": "1234"
        }
        ],
        "id": "d1a4f010-48d9-434b-9b3a-2d2b12f5e38c"
    }
    

    Regular Expression to extract the second id values

    Regular Expression formats

    ],\s\s\s\s\s"id": "(.+)"
    

    Note

    The above regex extract the following id values
    d1a4f010-48d9-434b-9b3a-2d2b12f5e38c
    
    0 讨论(0)
  • 2021-01-17 03:24

    You can use a lazy regex to find the guid directly instead of finding "id"

    Something like this: ([0-9a-z-]{36}).*?

    If you are not sure how to create the regex, just use an online regex maker.

    0 讨论(0)
提交回复
热议问题