I have a list of multi-row strings. I want to match first rows of those strings if they start with a variable number of digits NOT immediately followed by a period.
For example, a list might be
list = ["42. blabla \n foo", "42 blabla \n foo", "422. blabla \n foo"]
and my desired output would be 42 blabla
.
This code
import re
list = ["42. blabla \n foo", "42 blabla \n foo", "422. blabla \n foo"]
regex_header = re.compile("^[0-9]+(?!\.).*\n")
for str in list:
print(re.findall(regex_header, str))
outputs
['42. blabla \n']
['42 blabla \n']
['422. blabla \n']
This one works only with exactly two digits in the beginning of the string:
import re
list = ["42. blabla \n foo", "42 blabla \n foo", "422. blabla \n foo"]
regex_header = re.compile("^[0-9]{2}(?!\.).*\n")
for str in list:
print(re.findall(regex_header, str))
Output:
[]
['42 blabla \n']
['422. blabla \n']