I am trying to parse string from python regex-Not able get desired output

Viewed 56
sample_string = ":61:2002190219C45612.4S202EXC:OL3654628815//CT56748005:86:/BENM/Unitech Imports/REM//58970.047:61:2002190219C30000S103LCADV5674920204//CT56748006:86:/BENM/Gravity Imports/REM//INV:/FEB20/446301:61:2002190219C45612.4S202EXCOL3654628825//CT56748005:60F:61:2002190219C45612.4S202EXCOL3654628815//CT56748018"

Basically I need all

1. :61:, :86: string which are next to each other any where in the full message ex:- re.findall()=expected o/p - [':61:2002190219C45612.4S202EXC:OL3654628815//CT56748005:86:/BENM/Unitech Imports/REM//58970.047', :61:2002190219C30000S103LCADV5674920204//CT56748006:86:/BENM/Gravity Imports/REM//INV:/FEB20/446301] 
i have below regex for above case which is working fine, can we simplify a bit, 
61:(?:[\w /,.-]|:(?!61:|86:))*:86:(?:[A-Za-z0-9 /.-]|:(?!61:|86:))*
regex example - https://regex101.com/r/U3MWF7/4

2. All :61 string which are not followed by :86 anywhere in the full message, ex:- re.findall()=
expected o/p=[':61:2002190219C45612.4S202EXCOL3654628825//CT56748005',':61:2002190219C45612.4S202EXCOL3654628815//CT56748018']

I have below regex to get all :61 strings from message which is not giving correct result - its giving all :61 strings which are followed by:86 also.
61:(?:[\w /,.-]|:(?!61:|86:))*(?!:86:)* ()
regex example - https://regex101.com/r/2svNjG/1

I have tried multiple option but not able to get required output. request for help

1 Answers

You can match :61: and assert that what is on the right does not contain :86: Then match until the next occurrence of :61: by matching either one of the listed in [\w /.-] or match a : not directly followed by 61:

:61:(?!.*:86:)(?:[\w /.-]|:(?!61:)[\w /.-]*)*

Regex demo | Python demo

If there are multiple lines, you can make the dot match a newline using re.DOTALL or use an inline modifier (?s)

(?s):61:(?!.*:86:)(?:[\w /.-]|:(?!61:)[\w /.-]*)* 
Related