开发者

Regex to match multiple groups of text

开发者 https://www.devze.com 2023-03-10 23:01 出处:网络
I\'m not sure if this is possible in Regex but I want something like... Input: a=\"abc1\" ...some stuff...

I'm not sure if this is possible in Regex but I want something like...

Input:

a="abc1"
...some stuff...
a="def1"
...some stuff...
a="ghi1"
...some stuff...
b="123a"
...some stuff...
a="abc2"
...some stuff...
a="def2"
...some stuff...
a="ghi2"
...some stuff...
b="123b"
...some stuff...
a="abc3"
...some stuff...
a="def3"
...some stuff...
a="ghi3"
...some stuff...
b="123c"
...some stuff...

Wants:

match_1 = 123a
match_1_开发者_运维知识库1 = abc1
match_1_2 = def1
match_1_3 = ghi1

match_2 = 123b
match_2_1 = abc2
match_2_2 = def2
etc.

Attempt:

a="([^"]+)"[\D\W\S]+b="([^"]+)"

This would get the first "a=abc..." and the "b=123..." of each section but doesn't group "def..." and beyond.

The flavor of regex is the one in JMeter, which I believe is Perl.

Any suggestion or comment is appreciated, :)


Would this fit your need?

(?:a="([^"]+)".*?)(?:a="([^"]+)".*?)(?:a="([^"]+)".*?)b="([^"]+)"

You can see it here on Regexr

The Groups are in the order as they appear in the input

Group 1 = abc1
Group 2 = def1
Group 3 = ghi1
Group 4 = 123a

You need to turn on \s (dotall) modifier so that the . matches newlines. The (?:) are non capturing groups.

0

精彩评论

暂无评论...
验证码 换一张
取 消