mest
mest

Reputation: 33

Regex pattern to match all tags

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

Answers (2)

user557597
user557597

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

Avinash Raj
Avinash Raj

Reputation: 174696

You need to use a positive lookahead assertion.

([A-Z]{3})=(.*?)(?=[A-Z]{3}=|$)

DEMO

Upvotes: 2

Related Questions