Value extraction by REGEX - JMeter

2019-07-22 15:05发布

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

4条回答
放我归山
2楼-- · 2019-07-22 15:07

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
查看更多
别忘想泡老子
3楼-- · 2019-07-22 15:20
(\w+)(?:-|$)

Try this.This should do it.See demo.

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

查看更多
Deceive 欺骗
4楼-- · 2019-07-22 15:21

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

查看更多
Root(大扎)
5楼-- · 2019-07-22 15:29

Regular expression extractor can be used as,

enter image description here

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

will be the output

查看更多
登录 后发表回答