我從一個字符串中提取跟隨者數字,如下面的數字。第一種模式似乎不適用於單個數字。是否因爲第一個模式檢查數字後面是否有字符,並且第一行沒有其他字符4
和Followers
之間?第二種模式工作得很好。提取跟隨者計數的正則表達式模式
import re
text = """
4 Followers
330 Followers
23.5k Followers
67k Followers
25m Followers
"""
print(re.compile(r'(\d.+) Followers').findall(text))
print(re.compile(r'(\d+|\d.+) Followers').findall(text))
你應該真的簡單地使用https://regex101.com/ – Idos
只是分割空間,並獲得第一個數組.. – StefansArya
@Idos這是我用過的。剛接觸正則表達式並試圖理解我提出的更好的模式。 –