我需要在罗马数字上拆分文本。
这是我的文本
This is the part (a) of question number one. i. This is sub part one of part (a) question one ii. This is sub part two of part (a) question one iii. This is sub part three of part (a) question one
其实这是试卷中的一个部分,我怎么想把它分解成下面这样。
This is the part (a) of question number one.
This is sub part one of part (a) question one
This is sub part two of part (a) question one
This is sub part three of part (a) question one
所以在这里我想要的是,把句子分成罗马数字。
这是我写的正则表达式
text = This is the part (a) of question number one. i. This is sub part one of part (a) question one ii. This is sub part two of part (a) question one iii. This is sub part three of part (a) question one
for m in re.split(r' [a-z]+\. ',text):
print(m)
这是我得到的
This is the part (a) of question number one.
i. This is sub part one of part (a) question one
This is sub part two of part (a) question one
This is sub part three of part (a) question one
我的表达式适用于罗马数字二和三,但不适用于罗马数字一。
所以我需要一个适合任何罗马数字的通用表达式。
需要注意的重要一点是在罗马数字之前数字有一个空格,罗马数字后有一个句号,然后是一个空格。
有人可以帮我解决这个问题吗?
请您参考如下方法:
你的正则表达式捕获了子串one.,试着这样改:
text = 'This is the part (a) of question number one. i. This is sub part one of part (a) question one ii. This is sub part two of part (a) question one iii. This is sub part three of part (a) question one'
for m in re.split(r' [MDCLXVI]+\. ', text, flags=re.IGNORECASE):
print(m)
