gpt4 book ai didi

python - 我正在尝试从 python regex 解析字符串-无法获得所需的输出

转载 作者:行者123 更新时间:2023-12-04 08:58:01 25 4
gpt4 key购买 nike

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"
基本上我需要所有
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
我尝试了多个选项,但无法获得所需的输出。请求帮助

最佳答案

您可以匹配 :61: 并断言右边的内容不包含 :86: 然后匹配直到下一次出现 :61: 通过匹配 [\w /.-] 中列出的任何一个来匹配或匹配一个 :不直接跟 61:

:61:(?!.*:86:)(?:[\w /.-]|:(?!61:)[\w /.-]*)*
Regex demo | Python demo
如果有多行,您可以使用 re.DOTALL 使点匹配换行符或使用内联修饰符 (?s)
(?s):61:(?!.*:86:)(?:[\w /.-]|:(?!61:)[\w /.-]*)* 

关于python - 我正在尝试从 python regex 解析字符串-无法获得所需的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63705888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com