TLDR: subprocess.check_output('pcregrep', '-M', '-e', pattern, file) のエントリのリストを作成するきれいな方法はありますか?
私は python を使っsubprocess.check_output()
て呼び出していますpcregrep -M
。通常、呼び出して結果を分離しますsplitlines()
が、複数行のパターンを探しているので、うまくいきません。リストの各エントリが個々の一致するパターンである、一致するパターンのリストを作成するクリーンな方法を見つけるのに苦労しています。
これは、私がpcgrepしている簡単なサンプルファイルです
module test_module(
input wire in0,
input wire in1,
input wire in2,
input wire cond,
input wire cond2,
output wire out0,
output wire out1
);
assign out0 = (in0 & in1 & in2);
assign out1 = cond1 ? in1 & in2 :
cond2 ? in1 || in2 :
in0;
ここに私のpythonコード(の一部)があります
#!/usr/bin/env python
import subprocess, re
output_str = subprocess.check_output(['pcregrep', '-M', '-e',"^\s*assign\\s+\\bout0\\b[^;]+;",
"/home/<username>/pcregrep_file.sv"]).split(';')
# Print out the matches
for idx, line in enumerate(output_str):
print "output_str[%d] = %s" % (idx, line)
# Clear out the whitespace list entries
output_str = [line for line in output_str if re.match(\S+, line)]
ここに出力があります
output_str[0] =
assign out0 = in0 & in1 & in2
output_str[1] =
assign out1 = cond1 ? in1 & in2 :
cond2 ? in1 || in2 :
in0
output_str[2] =
のようなことができればいいのですが
output_list = subprocess.check_output('pcregrep', -M, -e, <pattern>, <file>).split(<multiline_delimiter>)
クリーンアップするためのガベージ (空白リスト エントリ) を作成したりsplit()
、パターンに依存しない区切り文字を使用したりする必要はありません。
一致する複数行パターンのリストを作成するきれいな方法はありますか?