【问题标题】:Search for Pattern in list : python Regex在列表中搜索模式:python 正则表达式
【发布时间】:2021-02-09 09:38:42
【问题描述】:

在数据分析并获得所需结果后,我将该结果附加到列表
现在我需要检索或分离结果(搜索模式并获取它)

代码:

data = []
data.append('\n'.join([' -> '.join(e) for e in paths]))

列表包含此数据:
CH_Trans -> St_1 -> WDL
TRANSFER_Trn -> St_1
Access_Ltd -> MPL_Limited
IPIPI -> TLC_Pvt_Ltd
234er -> Three_Star_Services -> Asian_Pharmas -> PPP_Channel
Sonata_Ltd -> Three_Star_Services
Arc_Estates -> Russian_Hosp
A -> B -> C -> D -> E -> F
G -> H
ZN_INTBNKOUT_SET -> -2008_1 -> X
ZZ_1_ -> AA_2 -> AA_3 -> ZZ_1_
XYZ- -> ABC -> XYZ-
SSS -> BBB -> SSS
Rock_8CC -> Russ -> By_sus -> Rock_8CC

Note : Display or Retrieve Pattern Which has more than two symbol of type[->]
( Txt -> txt -> txt )

我正在尝试通过正则表达式完成它

for i in data:
    regex = ("\w+\s->\s\w+\s->\s\w+")             
    match = re.findall(regex, i,re.MULTILINE)
    print(match)

Regex Expression I Tried But Unable to get Requried Result
#\w+\s->\s\w+\s->\s\w+
#\w+\s[-][>]\s\w+\s[-][>]\s\w+
#\w+\s[-][>]\s\w+\s[-][>]\s\w+\s[-][>]\s\w+

Result I Got
['CH_Trans-> St_1-> WDL', '234er -> Three_Star_Services -> Asian_Pharmas',
 'A -> B -> C', 'D -> E -> F', 'ZZ_1_ -> AA_2 -> AA_3', 
'SSS -> BBB -> SSS', 'Rock_8CC -> Russ -> By_sus']

需要的结果我要获取的是

----Pattern I------
CH_Trans -> St_1 -> WDL
234er -> Three_Star_Services -> Asian_Pharmas -> PPP_Channel
A -> B -> C -> D -> E -> F
ZN_INTBNKOUT_SET -> -2008_1 -> X


# Pattern II Consists of Patterns which are same i.e[ Fist_ele & Last_Ele Is Same]
----Pattern II------
ZZ_1_ -> AA_2 -> AA_3 -> ZZ_1_
XYZ- -> ABC -> XYZ-
SSS -> BBB -> SSS
Rock_8CC -> Russ -> By_sus -> Rock_8CC

【问题讨论】:

    标签: python-3.x regex pattern-matching


    【解决方案1】:

    请您尝试以下作为起点:

    regex = r'^\S+(?:\s->\s\S+){2,}$'
    for i in data:
        m = re.match(regex, i)
        if (m):
            print(m.group())
    

    结果(模式 I + 模式 II):

    CH_Trans -> St_1 -> WDL
    234er -> Three_Star_Services -> Asian_Pharmas -> PPP_Channel
    A -> B -> C -> D -> E -> F
    ZN_INTBNKOUT_SET -> -2008_1 -> X
    ZZ_1_ -> AA_2 -> AA_3 -> ZZ_1_
    XYZ- -> ABC -> XYZ-
    SSS -> BBB -> SSS
    Rock_8CC -> Russ -> By_sus -> Rock_8CC
    

    正则表达式^\S+(?:\s->\s\S+){2,}$的解释:

    ^\S+       start with non-blank string
    (?: ... )  grouping
    \s->\s\S+  a blank followed by "->" followed by a blank and non-blank string
    {2,}       repeats the previous pattern (or group) two or more times
    $          end of the string
    

    关于模式二,请说:

    regex = r'^(\S+)(?:\s->\s\S+){1,}\s->\s\1$'
    for i in data:
        m = re.match(regex, i)
        if (m):
            print(m.group())
    

    结果:

    ZZ_1_ -> AA_2 -> AA_3 -> ZZ_1_
    XYZ- -> ABC -> XYZ-
    SSS -> BBB -> SSS
    Rock_8CC -> Russ -> By_sus -> Rock_8CC
    

    正则表达式r'^(\S+)(?:\s->\s\S+){1,}\s->\s\1$'的解释:

    - ^(\S+)     captures the 1st element and assigns \1 to it
    - (?: ... )  grouping
    - \s->\s\S+  a blank followed by "->" followed by a blank and non-blank string
    - {1,}       repeats the previous pattern (or group) one or more times
    - \s->\s\1   a blank followed by "->" followed by a blank and the 1st element \1
    - $          end of the string
    

    为了得到模式 I 的结果,我们可能需要从第一个结果中减去模式 II 的列表。
    如果我们可以说:

    regex = r'^(\S+)(?:\s->\s\S+){2,}(?<!\1)$'
    

    它将排除最后一个元素与第一个元素不同的字符串,然后我们可以直接获得模式 I 的结果,但正则表达式导致错误说 "group references in lookbehind assertions" 到目前为止。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      相关资源
      最近更新 更多