Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有人知道使用python组合正则表达式的好方法吗?_Python_Regex - Fatal编程技术网

有人知道使用python组合正则表达式的好方法吗?

有人知道使用python组合正则表达式的好方法吗?,python,regex,Python,Regex,我是一个编程新手,但我做了大量的搜索,似乎找不到任何东西让我走上正轨。我有一个很大的正则表达式列表。如果可能的话,我需要找到一种方法来结合这些。我的正则表达式只有数字 例如,我的列表如下所示 832118[0-3] 832118[7-8] 832119[0-1] 832119[4-6] 832119[8-9] 8321206 832120[0-4] 832120[8-9] 832118[0-37-8] 832119[0-14-68-9] 832120[0-468-9] 我期望的输出如下所示

我是一个编程新手,但我做了大量的搜索,似乎找不到任何东西让我走上正轨。我有一个很大的正则表达式列表。如果可能的话,我需要找到一种方法来结合这些。我的正则表达式只有数字

例如,我的列表如下所示

832118[0-3]
832118[7-8]
832119[0-1]
832119[4-6]
832119[8-9]
8321206
832120[0-4]
832120[8-9]
832118[0-37-8]
832119[0-14-68-9]
832120[0-468-9]
我期望的输出如下所示

832118[0-3]
832118[7-8]
832119[0-1]
832119[4-6]
832119[8-9]
8321206
832120[0-4]
832120[8-9]
832118[0-37-8]
832119[0-14-68-9]
832120[0-468-9]
感谢您提供的任何提示

大宗报价


使用defaultdict和这个简单的正则表达式:

如果要匹配除数字[Numbers]以外的格式,则必须更改正则表达式

import re
from collections import defaultdict
dct = defaultdict(str)
data = ['832118[0-3]', '832118[7-8]', '832119[0-1]', '832119[4-6]', '832119[8-9]', '8321206', '832120[0-4]', '832120[8-9]']
for line in data:
    mtch = re.findall(r"(\d+)\[(\d+-\d+)\]", line)
    if mtch:
        dct[mtch[0][0]] += mtch[0][1]

for i, j in dct.items():
    print(i, '['+ j + ']')
输出:

832118 [0-37-8]
832120 [0-48-9]
832119 [0-14-68-9]

你确定正则表达式是这里使用的正确工具吗?是的。我在第三方系统中使用这些表达式。这与我希望做的非常接近。我看到的唯一问题是它没有捕获数字8321206并将其添加到表达式中,如832120[0-468-9]。我会继续玩它。非常感谢您的回复!