1

I have a list of dates and times in the following format: 25/07/201711:00:00 I just want to insert whitespace between the date and time so it looks like: 25/07/2017 11:00:00

The string replace method works well but is not very robust i.e. mystring.replace("2017","2017 " ) works but only for 2017 dates. Regex sub method seems to be what I need to use but have not been successful so far. Any suggestions would be very helpful as my regex knowledge is limited.

This is closest from what I have tried:

>>>re.sub(r'20[0-9][0-9]', r'20[0-9][0-9] ', s)
'20[0-9][0-9] 04:00'

4 Answers 4

7

If your dates are in 'dd/mm/yyyy' format, why can't you just index your string?

>>> mystring[:10] + ' ' + mystring[10:]
'25/07/2017 11:00:00'
Sign up to request clarification or add additional context in comments.

1 Comment

Great idea, wish i thought of that!
3

One way would be to use lookarounds:

(?<=\d{4})(?=\d{2}:)

This needs to be replaced by a whitespace, see a demo on regex101.com.


In Python this would be

import re

date = "25/07/201711:00:00"
date = re.sub(r'(?<=\d{4})(?=\d{2}:)', ' ', date)
print(date)
# 25/07/2017 11:00:00

As seen in the comments section, if the date is always of the same format, one might better slice the strings.

5 Comments

Too complicated for this case, since the strings are of equal length.
Forgive me for being pernickety, but unless the OP responds saying that the dates are not always formatted the same way, I don't think we should be suggestion complicated regular expressions when a few slices will do.
@SwiftsNamesake: You are right that slicing will be the more efficient and straighter approach in this case. I'll however leave the answer for e.g. not well formatted dates.
Removed downvote. I'd welcome a clarifying edit, but that's obviously not my call (also, see the comments on @Alexander's answer).
*should be suggesting
1

You don't need a regular expression for this, assuming that the dates are padded, so that they're all the same length (which seems to be the case).

>>> date = '25/07/201711:00:00'
>>> n    = len('dd/mm/yyyy') # Splitting index (easier to understand than a magic constant)
>>> print(date[:n] + ' ' + date[n:])
25/07/2017 11:00:00

Comments

0

The best way is to match the full date and time in two groups, for instance like this:

import re
regex = r"(\d{2}/\d{2}/\d{4})(\d{2}:\d{2}:\d{2})"
re.sub(regex, r"\1 \2", "25/07/201711:00:00")
# -> '25/07/2017 11:00:00'

3 Comments

Maybe I'm too harsh (the OP mentions re:s in the title), but this is much more complicated and inefficient than it needs to be, assuming the strings are formatted the same way.
@SwiftsNamesake Yes, certainly. But we don't have the whole story and RegEx can be the right solution.
Too late to change the vote unless you edit. I'll hold out for the OPs response, then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.