我想使用正则表达式从Igbo文本中提取形式为wor'word的单词(我真的不太了解正则表达式)。例如,

line = "jir’ọbara ya"


如果我做

found = re.match("\w+’\w+", line)
print found.group()


我得到'NoneType' object has no attribute 'group'而不是jir’ọbara

然后,如果我执行found = re.match("\w+’|\w+", line),它只会给我jir’

关于如何解决此问题或最好的其他方法的任何建议?谢谢。

最佳答案

如果行格式一致,则:

wor, word = line.split()[0].split("’")


要么

>>> found = re.match("(\w+)’(\w+)", line)
>>> found.group(1)
'jir'
>>> found.group(2)
'ọbara'
>>>

10-06 10:42
查看更多