ayush koirala
ayush koirala

Reputation: 3

How to match a string after colon on Regex

text = Bounding box for object 1 "PASpersonWalking" (Xmin, Ymin) - (Xmax, Ymax) : (160, 182) - (302,431)

I need only : (160, 182) - (302,431) from the text

Upvotes: 0

Views: 281

Answers (1)

Michael Bianconi
Michael Bianconi

Reputation: 5242

rgx = r'^.+:(.+)$'
re.search(rgx, text).group(1)

^: Start at beginning of string

.+:: Allow any characters until a colon

(.+)$: Capture all characters until the end of the string

To put it in the format x,x,x,x:

>>> rgx = r'^.+: \((.+),\s*(.+)\).+\((.+),\s*(.+)\)$'
>>> m = re.search(rgx, text)
>>> result = f'{m.group(1)},{m.group(2)},{m.group(3)},{m.group(4)}'
>>> result
'160,182,302,431'
>>> 

Upvotes: 1

Related Questions