https://regex101.com/r/Puggjm/5
我基本上是在尝试忽略所有行号,后跟空格或什么都不做。我当前的正则表达式:^[\d\s].+(?:[A-Z\s]*)*$
实际上不忽略行号,后面没有任何内容。
答案 0 :(得分:1)
您可以使用否定的前瞻来断言其后不是1+位数字,后跟0+倍的空白字符:
^(?!\d+\s*$)\d+.+$
^
字符串的开头(?!\d+\s*$)
断言右边的否定表示不是1+位数字,后跟0+倍的空格字符和字符串的结尾\d+.+
匹配数字1+次和任何字符1+次$
字符串的结尾请参见regex demo | Python demo
使用findall的示例:
import re
regex = r"^(?!\d+\s*$)\d+.+$"
test_str = ("Here goes some text. {tag} A wonderful day. It's soon cristmas.\n"
"2 Happy 2019, soon. {Some useful tag!} Something else goes here.\n"
"3 Happy ending. Yeppe! See you.\n"
"4\n"
"5 Happy KKK!\n"
"6 Happy B-Day!\n"
"7\n"
"8 Universe is cool!\n"
"9\n"
"10 {Tagish}.\n"
"11\n"
"12 {Slugish}. Here goes another line. {Slugish} since this is a new sentence.\n"
"13\n"
"14 endline.")
print(re.findall(regex, test_str, re.MULTILINE));
当数字后有一个点时,您可以使用:
答案 1 :(得分:0)
您的正则表达式仅匹配1位数字,请用this simplified version进行更改:
^\d+\b.+$