這裏是@ stribizhev的解決方案,它的工作就像魅力。
^\s*(?:\b[a-zA-Z]+\b\s*)+(?=[:-])
對於像我這個新手是溶液的一個簡單的解釋:在一個新行,接着
> ^\s Anchor to the white space (tab, newline, blankspace)
> * Look for repetition of white space if any
> (?: Start a non-capturing group
> \b Look for a word
> [a-zA-Z] Anyword that starts with either a capital or small alphabet
> + Look for repeating capital/small alphabets
> \s* Match a blank space after the word
>)+ Repeat
> (?= Ignore what follows (my best guess)
> [:-] Look for ':' or '-'
在本質上該正則表達式查找字的組「:」或「 - 」
加入{N,M}爲
^[\s]*(?:\b[a-zA-Z]+\b[\s]*){1,3}(?=[:-])
作出上述表示聰明,告訴它超越你開始多少字丟失的相關信息10
['^ \ s *(?:\ b [a-zA-Z] + \ b \ s *)+(?= [: - ])'](https://regex101.com/r/uF9zU8/2)? –
'^ \ s *(?:(?=。+ [: - ] \ s * $)(?:\ b [AZ] [az] * \ b [\ s] *)+)'應該可以工作 –
使用正則表達式? ():myword [0] == myword [0] .upper()和myword [0] .isalpha():#做點什麼' –