Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
where is a pattern?
#8
That's pattern is way longer than it need to be,have you look into the basic regex before?
Quick test.
>>> import re
>>> 
>>> text = "foo 2019-06-12.123 bar"
>>> r = re.search(r"\d{4}-\d{2}-\d{2}\.\d{3}", text).group()
>>> r
'2019-06-12.123'
If it's a common valid date format can parse with dateutil as mention bye @Gribouillis
I like pendulum the best(and most correct) date tool that's is made for Python in the latest years.
>>> import pendulum
>>> 
>>> d = '2019-06-12'
>>> pendulum.parse(d)
DateTime(2019, 6, 12, 0, 0, 0, tzinfo=Timezone('UTC')
It will fail on auto parse with 2019-06-12.123,but can write a own with formatter.
>>> import pendulum
>>> 
>>> dt = pendulum.from_format('2019-06-12.123', 'YYYY-DD-MM.hms')
>>> dt
DateTime(2019, 12, 6, 1, 2, 3, tzinfo=Timezone('UTC'))
As you see pendulum dos this way better than strptime().
Reply


Messages In This Thread
where is a pattern? - by Skaperen - Jun-07-2019, 05:07 AM
RE: where is a pattern? - by heiner55 - Jun-07-2019, 05:16 AM
RE: where is a pattern? - by Skaperen - Jun-07-2019, 05:37 AM
RE: where is a pattern? - by heiner55 - Jun-07-2019, 06:55 AM
RE: where is a pattern? - by Skaperen - Jun-07-2019, 01:08 PM
RE: where is a pattern? - by Gribouillis - Jun-07-2019, 08:53 AM
RE: where is a pattern? - by heiner55 - Jun-07-2019, 01:38 PM
RE: where is a pattern? - by snippsat - Jun-07-2019, 04:25 PM
RE: where is a pattern? - by Skaperen - Jun-07-2019, 11:57 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020