Reputation: 6658
These are some of my test Cases:
{APIDETAILS=FOO, BAR, SING, RUN, OP1/OPSUB1/RESULT=SOMETHING, OP1/OPSUB2/RESULT=SOMETHING, OP2/OPSUB1/RESULT=SOMETHING}
{APIDETAILS=FOO, OP1/OPSUB1/RESULT=SOMETHING, OP1/OPSUB2/RESULT=SOMETHING, OP2/OPSUB1/RESULT=SOMETHING}
{APIDETAILS=FOO, O.P.OP3/OPSUB1/RESULT=SOMETHING, OP1/OPSUB2/RESULT=SOMETHING, OP2/OPSUB1/RESULT=SOMETHING}
{APIDETAILS=FOO, OP.PO.OP4/OPSUB1/RESULT=SOMETHING, OP1/OPSUB2/RESULT=SOMETHING, OP2/OPSUB1/RESULT=SOMETHING}
{OP1/OPSUB1/RESULT=SOMETHING, OP1/OPSUB2/RESULT=SOMETHING, OP2/OPSUB1/RESULT=SOMETHING, APIDETAILS=FOO}
{OP1/OPSUB1/RESULT=SOMETHING, OP1/OPSUB2/RESULT=SOMETHING, OP2/OPSUB1/RESULT=SOMETHING, APIDETAILS=FOO, SING, BAR}
{OP1/OPSUB1/RESULT=SOMETHING, OP1/OPSUB2/RESULT=SOMETHING, OP2/OPSUB1/RESULT=SOMETHING, APIDETAILS=FOO, BAR, SING
Note: '}' is intentionally missing in the last line.
What I want to match: Everything followed by APIDETAILS, but only until end of APIDETAILS. The end if clearly not defined (look for above test cases for different scenarios)
The Regex I came up with:
(?:APIDETAILS=)(.*?)(?:}|\/|$)
What I'm able to match:
Question: How do I get rid of some noise in matches 1,2,3,4 above and end up having only with the following?
What I need to match:
Upvotes: 1
Views: 50
Reputation: 70732
Use a Positive Lookahead:
APIDETAILS=(.*?)(?=}|,\s*\S+=|$)
Or simply add to your non-capturing group:
APIDETAILS=(.*?)(?:}|,\s*\S+=|$)
Upvotes: 2
Reputation: 41838
Use this:
(?m)(?<=APIDETAILS=).*?(?=,\s*\S+=|}|$)
See the matches in the regex demo.
(?m)
turns on multi-line mode, allowing ^
and $
to match on each line(?<=APIDETAILS=)
asserts that what precedes is APIDETAILS=
.*?
lazily matches chars up to...(?=,\s*\S+=|}|$)
can assert that what follows is a comma followed by optional whitespace, non-space chars and =
, OR |
the }
character OR the end of the line $
Upvotes: 2