簡體   English   中英

如何檢測編號標題中的編號(長度)?

[英]How to detect (length of) numbering in a numbered title?

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)

也許正則表達式可以幫助您?

如果編號始終在開頭並用空格分隔。

len(title.split()[0])

應該管用。

再次考慮,也許您可​​以執行title.split()[0]並使用正則表達式檢查結果。 如果它滿足標題的定義,請檢查長度,否則返回0

如果您首先嘗試使用正則表達式來檢測數字,則可以嘗試類似的操作

返回正則表達式match()在Javascript中的位置?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM