tl;dr
https://repl.it/repls/AbandonedSoulfulNetbsd (所有代码)。
正则表达式:
https://regex101.com/r/Puggjm/1/
编辑: 执行时的正则表达式仅显示:1 个匹配项,n 个步骤。而不是匹配的数量来对应整个文本中实际找到的行号的数量。
主.py if __name__ == '__main__':
# See text content in the textfile.txt,
# https://repl.it/repls/AbandonedSoulfulNetbsd
pattern = re.compile(r'^[0-9].*', re.M | re.S)
for m in re.finditer(pattern, text):
print(m.group(0))
if __name__ == '__main__':
# See text content in the textfile.txt,
# https://repl.it/repls/AbandonedSoulfulNetbsd
pattern = re.compile(r'^[0-9].*', re.M | re.S)
for m in re.finditer(pattern, text):
print(m.group(0))
想要结果
好吧,我想抓取所有实际以数字开头的行(不管数字是什么),但是,我不知道是否有正则表达式分别获取每一行的方法?或者,应该改用 re.split() 之类的东西吗?
更新:
这个,^[0-9](.*?)$ 正则表达式模式给了我 13 个匹配项, https://regex101.com/r/Puggjm/2/ - 但是,我仍然无法让它在 python 中工作。
请您参考如下方法:
你需要摆脱 s 标志,可以使用 \d 代替:
^\d.*$
单行标志改变了点的行为,因为它现在也匹配换行符,因此 - 使用你的量词 - 一切。 \d 包括 [0-9](以及其他一些数字)。参见 your modified demo .
