【问题标题】:How to detect (length of) numbering in a numbered title?如何检测编号标题中的编号(长度)?
【发布时间】: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


【解决方案1】:

如果编号总是在开头并用空格分隔。

len(title.split()[0])

应该可以。

再想一想,也许您可​​以使用title.split()[0] 并使用您的正则表达式检查该结果。如果满足你对标题的定义,检查长度,否则返回0

【讨论】:

    【解决方案2】:

    如果您尝试使用类似的方法首先使用正则表达式来检测数字

    Return positions of a regex match() in Javascript?

    【讨论】:

      猜你喜欢
      • 2011-01-22
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 2019-05-26
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多