简体   繁体   中英

Regex a: b c d a:b

I currently have the regex '([^: ]+):([^ ]+)?' , which when given a string like This is a correct:test msg: abc bool:y returns [('correct', 'test'), ('msg', ''), ('bool', 'y')] (using pythons re.findall ).

I actually want it to return something like [('correct', 'test'), ('msg', 'ab c'), ('bool', 'y')] . How do I force regex to look inside the second chunk, and see if theres a : in it?

您可以在第二部分使用正向前瞻 ,例如

'([^ :]+): *(.+?)?(?:(?= [^ ]+:)|$)'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM