正则表达式与单词匹配并与匹配的连接

regular expression match with word and join with matched

我遇到这个问题,好久都找不到解决办法。我的猜测是最好的方法是通过 re,但我愿意接受任何建议。

我在列表中有以下文本,我将其写入文本文件以供其他程序导入以进行某些计算。

问题是我必须包含具有类似 []s=0 的匹配行(即 []s=0),如下所示:[]s=0 -> p01:(s'=1) + p02 :(s'=2)。因此,前两行必须由 + 运算符组合,接下来的第三和第四行等等。

module main
[]s=0 -> p01:(s'=1);
[]s=0 -> p02:(s'=2);
[]s=1 -> p10:(s'=0);
[]s=1 -> p12:(s'=2);
[]s=2 -> p20:(s'=0);
[]s=2 -> p23:(s'=3);
[]s=3 -> p34:(s'=4);
[]s=4 -> p40:(s'=0);
[]s=4 -> p45:(s'=5);
[]s=4 -> p46:(s'=6);
[]s=5 -> p57:(s'=7);
[]s=6 -> p67:(s'=7);
[]s=7 -> p70:(s'=0);
endmodule

假设您的数据是字符串?

data = ["[]s=0 -> p01:(s'=1);",
"[]s=0 -> p02:(s'=2);",
"[]s=1 -> p10:(s'=0);",
"[]s=1 -> p12:(s'=2);",
"[]s=2 -> p20:(s'=0);",
"[]s=2 -> p23:(s'=3);",
"[]s=3 -> p34:(s'=4);",
"[]s=4 -> p40:(s'=0);",
"[]s=4 -> p45:(s'=5);",
"[]s=4 -> p46:(s'=6);",
"[]s=5 -> p57:(s'=7);",
"[]s=6 -> p67:(s'=7);",
"[]s=7 -> p70:(s'=0);"]

dict_ = {}
for item in data:
    split = (item.split("=")[1].split(" ")[0])
    if not split in dict_:
        dict_[split] = item
    else:
        dict_[split] = dict_[split] + " + " + item

for key, value in dict_.items() :
    print (key, value)