Value extraction by REGEX - JMeter

2019-07-22 14:45发布

问题:

I want to extract each part of the following serial code delimitered by dash '-' in separated Regular Expression Extractors in JMeter.

Serial code: cdaf57ce-1a50-42fc-b548-2c84ad7911a8

The expected result is:

Regular Expression Extractor 1: cdaf57ce
Regular Expression Extractor 2: 1a50
Regular Expression Extractor 3: 42fc
Regular Expression Extractor 4: b548
Regular Expression Extractor 5: 2c84ad7911a8

I tried the following regexes for each:

Regular Expression Extractor 1: (\w{8})-
Regular Expression Extractor 2: [^(\w{8})]-(\w{4})-[^(\w{4})]-[^(\w{4})]-[^(\w{12})]
Regular Expression Extractor 3: [^(\w{8})]-[^(\w{4})]-(\w{4})-[^(\w{4})]-[^(\w{12})]
Regular Expression Extractor 4: [^(\w{8})]-[^(\w{4})]-[^(\w{4})]-(\w{4})-[^(\w{12})]
Regular Expression Extractor 5: -(\w{12})

First and last ones worked correctly but others didn't.

Thanks

回答1:

Regular expression extractor can be used as,

user_variables_g1 = cdaf57ce
user_variables_g2 = 1a50
user_variables_g3 = 42fc
user_variables_g4 = b548
user_variables_g5 = 2c84ad7911a8

will be the output



回答2:

(\w+)(?:-|$)

Try this.This should do it.See demo.

http://regex101.com/r/gJ4uX1/2



回答3:

A simple regex like :

([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})

should work fine - and the individual parts can be obtained from the captured groups within the match.

If you must use individual regular expressions for each part then you'd need to do something like

  1. \b[0-9a-f]{8}
  2. (?<=[0-9a-f]{8}-)[0-9a-f]{4}
  3. (?<=[0-9a-f]{8}-[0-9a-f]{4}-)[0-9a-f]{4}
  4. (?<=[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-)[0-9a-f]{4}
  5. (?<=[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-)[0-9a-f]{12}

this is using a lookbehind, which is not the most efficient construct, (and not supported in all regex engines), but should work. \b matches a word boundary

Lookaheads are another way - and better supported:

  1. [0-9a-f]{8}(?=-)
  2. [0-9a-f]{4}(?=-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b)
  3. [0-9a-f]{4}(?=-[0-9a-f]{4}-[0-9a-f]{12}\b)
  4. [0-9a-f]{4}(?=-[0-9a-f]{12}\b)
  5. [0-9a-f]{12}\b


回答4:

Are you using one field to validate this Serial code try this Simple Regex:

^[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}$

Matches:

cdaf57ce-1a50-42fc-b548-2c84ad7911a8

CDAF57CE-1A50-42FC-B548-2C84AD7911A8

12asCda1-23ON-324f-Bse3-htskjas1234q