我有一個代碼,用於分組中的單詞,如果它有paranthesis前有相同的名稱。分組使用正則表達式冒號':'
如:
car __name__(skoda,audi)
car __name__(benz)
輸出:
car __name__(skoda,audi,benz)
但是當它亙古不變的輸出端設置一個冒號:
,
car __name__(skoda,audi): =>no output prints with :
car __name__(benz):
我認爲這個問題是我的正則表達式
我的代碼:
import collections
class Group:
def __init__(self):
self.members = []
self.text = []
with open('out.txt','r') as f:
groups = collections.defaultdict(Group)
group_pattern = re.compile(r'(\S+(?: __[^__]*__)?)\((.*)\)$')
current_group = None
for line in f:
line = line.strip()
m = group_pattern.match(line)
if m: # this is a group definition line
group_name, group_members = m.groups()
groups[group_name].members.extend(group_members.split(','))
current_group = group_name
for group_name, group in groups.items():
print "%s(%s)" % (group_name, ','.join(group.members))
「(\ S +(__ [^ __] * __)?)\((。*)\):?$」before':?'不需要我猜... – Anvesh 2014-08-30 06:43:29