How to extract string between space and symbol ‘>’?

String 1:

 [impro:0,grp:00,time:0xac,magic:0x00ac] CAR<7:5>|BIKE<4:0>,orig:0x8c,new:0x97

String 2:

[impro:0,grp:00,time:0xbc,magic:0x00bc] CAKE<4:0>,orig:0x0d,new:0x17

In string 1, I want to extract CAR<7:5 and BIKE<4:0,

In string 2, I want to extract CAKE<4:0

Any regex for this in Python?

Answer

You can use w+<[^>]+

DEMO

enter image description here

  • w matches any word character (equivalent to [a-zA-Z0-9_])
  • + matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy).
  • < matches the character <
  • [^>] Match a single character not present in the list
  • + matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)