【发布时间】:2018-12-27 18:54:45
【问题描述】:
python3 中是否有一种方法(经过训练的模型或确定性函数)可以返回标题中编号的长度。 例如,
"I. This is a big title" ---> length=len("I.")=2
"1.10 This a small title" ---> length=len("1.10")=4
"A)b) This is another title" ---> length=len("A)b)")=4
"C.2 This is a regular title" ---> length=len("C.2")=3
"This is not a title" ---> length=0
etc....
?
我写了一个小函数,它使用正则表达式来检测字符串是否以编号开头:
pattern = r'(^IX|IV|VI{0,3}|I{1,3})(\s|-|\s-|\)|\s\)|\.|\s\.|/|\s/|–|\s–)'
m_romans = re.search(pattern, text)
m_letters = re.search(r'^([a-zA-Z])(\s|-|\s-|\)|\s\)|\.|\s\.|/|\s/|–|\s–)', text)
m_digits = re.search(r'^(\d)(\s|-|\s-|\)|\s\)|\.|\s\.|/|\s/|–|\s–)', text)
也许正则表达式可以提供帮助?
【问题讨论】:
-
这归结为编写一个正则表达式,或功能上与正则表达式等效的东西,用于检测“编号模式”。已经有很多正则表达式教程,所以任何额外的帮助只会引导您明确您认为的“编号模式”是什么。
标签: python numbers string-length variable-length