正则表达式

时间:2018-06-22 06:43:42

标签: python regex

我需要为

匹配正则表达式

txt =“ orderType不在('connect','Modify','random','more')中)

正确数据:

txt =“ orderType在('connect')中”

txt =“ orderType不在('connect','Modify')”中

N个项目可以放在方括号内,引号和逗号如上分隔

所有其他字符都不应该匹配,如下所示

txt =“ orderType不在('connect',Modify,'ran = dom','more')中”

import re
pattern1 = '\w+\s+(?:is|not)\sin\s+\('
pattern2 = '\'\w+\''
pattern3 = '\s?,\s?'+pattern2+'+'
print(re.findall(pattern3, txt))
pattern6 = pattern1+pattern2
pattern5 = pattern1+pattern2+pattern3
pattern4 = (pattern2+ pattern3)  +'|'+ (pattern2 )
pattern = pattern5+ '|' + pattern6
print(re.findall(pattern,txt))

我的输出是[“ orderType不在('connect','Modify'”]

预期输出应为:orderType不在(“连接”,“修改”,“随机”,“更多”)

整行,我不介意是否所有匹配项都返回true,其余部分返回false

预先感谢

2 个答案:

答案 0 :(得分:0)

您缺少一些括号。当组合分别与字符串p1p2匹配的表达式s1s2时,由p1+p2生成的正则表达式不一定与s1+s2匹配,由于regexp语法的优先顺序。以下将完成您可能想要的操作(更改针对“ pattern3”和“ pattern”):

import re
pattern1 = '\w+\s+(?:is|not)\sin\s+\('
pattern2 = '\'\w+\''
pattern3 = '(?:\s?,\s?'+pattern2+')+'
print(re.findall(pattern3, txt))
pattern6 = pattern1+pattern2
pattern5 = pattern1+pattern2+pattern3
pattern = '(?:'+pattern5+ ')|(?:' + pattern6+')'
print(re.findall(pattern,txt))

我只在regexp字符串中添加了所需的(),没有其他修复方法。请注意,这与输入字符串的右括号不匹配-如果需要,请在末尾添加'\s+\)

答案 1 :(得分:0)

尝试一下:

import re

texts=[ "orderType not in ('connect', 'Modify', 'random', 'more')",
        "orderType is in ('connect')",
        "orderType not in ('connect', 'Modify')"
        ]

reg=re.compile( r"\s*orderType\s+(?:is|(not))\s+in\s+\(\s*'connect'\s*(?(1),\s*'Modify'\s*\)|\))" )
for txt in texts:
    m=reg.fullmatch(txt)
    print("matched -->" if m else "not matched -->",txt)

"""
Descriptions:
    (is|(not))      The external parentheses to encapsulate the '|' pattern (or);
    (?:is|(not))    ?: The encapsulated expression matching as a group, is not interesting to us as a whole.
    (not)           If matched, it will be the 1st group.
    \(              Escape sequence, it matches the '(' .
    (?(1),\s*'Modify'\s*\)|\)   Yes-no pattern (?(group_number)expr1|expr2)
"""