2
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

1 Answer 1

2

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 /.-]*)* 
Sign up to request clarification or add additional context in comments.

12 Comments

This is including :60F:, which is different string all together. :61:2002190219C45612.4S202EXCOL3654628825//CT56748005 :60F:
@Navneet So you don't want to include a 6 followed by a digit and optional char A-Z? regex101.com/r/DzoIcc/1 or a char a-z or digit regex101.com/r/Flrlq7/1
Not maching for full string regex101.com/r/Flrlq7/1
@Navneet What do you mean by full string? You get the 2 desired matches right?
Thanks @The fourth bird , still some of string are not matching but you have given enough pointers. i will look in that Thanks:)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.