Reputation: 33
I'm trying to write regex pattern to parse string with similar tags (3 chars) and those tags are retired in the string
ABC=TEXT 1 - HERE.. DEF=/TEXT 2: TEXT .. ZYX=TEXT 3 TEXT
When I use
#([A-Z]{3})=(.*)+#isU
I only get tags ABC, DEF, ... but didn't get content. How do I can get both?
I would like to get result with pairs tags and content
ABC
TEXT 1 - HERE..
DEF
/TEXT 2: TEXT ..
ZYX
TEXT 3 TEXT
Update: See my example at https://regex101.com/r/uI0fW4/1
Upvotes: 1
Views: 98
Reputation:
This ([A-Z]{3})=(.*)+
regex, specifically
this subexpression
(.*)+
tells the engine to overwrite capture group 1 as many times as it can.
On the last write, .*
matched nothing because it can match nothing.
Thus that capture group is empty.
You could use this instead to get data in capture group 2.
# (\b[A-Z]{3})=((?:(?!\b[A-Z]{3}=).)*)
( \b [A-Z]{3} ) # (1)
=
( # (2 start)
(?:
(?! \b [A-Z]{3} = )
.
)*
) # (2 end)
Upvotes: 1
Reputation: 174696
You need to use a positive lookahead assertion.
([A-Z]{3})=(.*?)(?=[A-Z]{3}=|$)
Upvotes: 2