0

I have a string like

some_string = """
15.10.2020 102000 sometext 11112 
16.11.2020 120888.89 sometext 22222
18.09.2020 1 fjjfjf sometext 
15.12.2020 109555.55 sometext 22222
"""

I should have an output like this

15.10.2020 102000
16.11.2020 120888.89
18.09.2020 1
15.12.2020 109555.55

I tried to use this method but regex not matched '1-length non-digit'

re.findall("\d{2}\.\d{2}\.\d{4}\s\d+\.{0,1}\d", some_string)
but had output
['15.10.2020 102000', '16.11.2020 120888.8', '15.12.2020 109555.5']

Then I tried to use change it like this

re.findall("\d{2}\.\d{2}\.\d{4}\s\d+\.{0,1}\D", some_string)
#in this case regex matched '1-length as digit' but not as a float
['15.10.2020 102000 ',
 '16.11.2020 120888.',
 '18.09.2020 5 ',
 '15.12.2020 109555.']

Could you help me to solve my problem?

  • 1
    Your regex to match the number is wrong as it requires at least two digits. Try `re.findall("\d{2}\.\d{2}\.\d{4}\s\d+(?:\.\d+)?", some_string)` or any of the other methods in the duplicate – Nick Feb 04 '21 at 03:34
  • No worries - I'm glad it was helpful – Nick Feb 04 '21 at 03:42

0 Answers0