Skip to main content
 首页 » 编程设计

python之在伊博语文本中查找缩略词模式 (*'*)

2025年02月15日1498°冷暖

我想使用正则表达式在伊博语文本中提取形式为 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' 
>>>