匹配列表中的一对一值

时间:2018-07-17 12:48:01

标签: python regex

下面是我正在运行的代码,它们分别获得const EMPTY_ARRAY = [] function mapStateToProps(state) { return { productList: state.products || EMPTY_ARRAY customerList: state.customer || EMPTY_ARRAY } } ,请让我知道如何获取output。我正在尝试一对一地匹配值,但是却正在一对一地匹配。

expected output

输出

data=["(array['AZ','XCA'] <@ type_list AND (array['jh-ee60','6E16'] <@ value_list OR array['jh-ee30','TO-IO'] <@ value_list)",
 "(array['Q','SSA'] <@ type_list AND array['3WE.5_%_WW21_','AQWW_662'] <@ value_list)",
 "(array['system','s_num'] <@ type_list AND (array['V378.63','KMSS'] <@ value_list OR array['D885.13','UASS'] <@ value_list OR array['DV5.1','R'] <@ value_list OR array['DV25.1','W'] <@ value_list)"]

import re
def rplceOR(d):

        r = re.compile(r"array\['(.*?)','(.*?)']")    
        for s in d:
            m = r.search(s)
            if m:                             
                array_vals = r.findall(s, s.index("<@ type_list"))
                if len(array_vals) > 0:
                    print ("(type_value LIKE ALL ({}))".format(" OR type_value LIKE ALL ".join(["'%{}={};%'".format(m.group(1), x) for x in array_vals])))

rplceOR(data)

预期产量

(type_value LIKE ALL ('%AZ=('jh-ee60', '6E16');%' OR type_value LIKE ALL '%AZ=('jh-ee30', 'TO-IO');%'))
(type_value LIKE ALL ('%Q=('3WE.5_%_WW21_', 'AQWW_662');%'))
(type_value LIKE ALL ('%system=('V378.63', 'KMSS');%' OR type_value LIKE ALL '%system=('D885.13', 'UASS');%' OR type_value LIKE ALL '%system=('DV5.1', 'R');%' OR type_value LIKE ALL '%system=('DV25.1', 'W');%'))

1 个答案:

答案 0 :(得分:1)

由于模式中现在有两个组,因此需要将两个值传递给format方法。

您可以使用

print("(type_value LIKE ALL ({}))".format(") OR type_value LIKE ALL (".join(["'%{0}={2};%','%{1}={3};%'".format(m.group(1), m.group(2), x, y) for x,y in array_vals])))

请参见Python demo

输出:

(type_value LIKE ALL ('%AZ=jh-ee60;%','%XCA=6E16;%') OR type_value LIKE ALL ('%AZ=jh-ee30;%','%XCA=TO-IO;%'))
(type_value LIKE ALL ('%Q=3WE.5_%_WW21_;%','%SSA=AQWW_662;%'))
(type_value LIKE ALL ('%system=V378.63;%','%s_num=KMSS;%') OR type_value LIKE ALL ('%system=D885.13;%','%s_num=UASS;%') OR type_value LIKE ALL ('%system=DV5.1;%','%s_num=R;%') OR type_value LIKE ALL ('%system=DV25.1;%','%s_num=W;%'))