簡體   English   中英

正則表達式

[英]regular expression

我需要匹配正則表達式

txt = "orderType not in ('connect', 'Modify', 'random', 'more')"

正確的數據:

txt = "orderType is in ('connect')"
txt = "orderType not in ('connect', 'Modify')"

括號內可以包含N個項目,引號和逗號如上分隔。 其余的都不應該匹配,如下所示

txt = "orderType not in ('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 not in ('connect', 'Modify'"]

預期的輸出應為: orderType not in ('connect', 'Modify', 'random', 'more')

就整行而言,我不介意所有匹配項返回true,其余返回false

您缺少一些括號。 當組合分別與字符串s1s2匹配的表達式p1p2 ,由於regexp語法中的優先順序,由p1+p2生成的regexp不一定與s1+s2匹配。 以下將完成您可能想要的操作(更改針對“ 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+\\)

試試吧:

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)
"""

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM