1

I have following options, but all of them return full string. I need to remove date at the beginning with regex.

d = re.match('^\d{2}.\d{2}.\d{4}(.*?)$', '01.10.2018Any text..')
d = re.match('^[0-9]{2}.[0-9]{2}.[0-9]{4}(.*?)$', '01.10.2018Any text..')

How to do that? Python 3.6

3 Answers 3

2

You could use sub to match the date like pattern (Note that that does not validate a date) from the start of the string ^\d{2}\.\d{2}\.\d{4} and replace with an empty string.

And as @UnbearableLightness mentioned, you have to escape the dot \. if you want to match it literally.

import re
result = re.sub(r'^\d{2}\.\d{2}\.\d{4}', '', '01.10.2018Any text..')
print(result) # Any text..

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

That is why I added (Note that that does not validate a date) to my answer.
Here the . matching any character works, however for educational purposes it would be good to escape the dot to show OP that metacharacters need escaping.
@UnbearableLightness Ah I missed that one. Thank you for your sharp observation!
1

Grab the first group of the match

>>> d = re.match('^\d{2}.\d{2}.\d{4}(.*?)$', '01.10.2018Any text..').group(1)
>>> print (d)
'Any text..'

If you are not sure, if there would be a match, you have to check it first

>>> s = '01.10.2018Any text..'
>>> match = re.match('^\d{2}.\d{2}.\d{4}(.*?)$', s)
>>> d = match.group(1) if match else s
>>> print(d)
'Any text..'

6 Comments

And if there is no match... won't it return an error?
Yes. AttributeError: 'NoneType' object has no attribute 'group'
So the problem is i should use group(1) instead of d[3]? That's weird why I can't use array to print result.
That would also work for 99.10.2018Any text.. which is an invalid date...
Here the . matching any character works, however for educational purposes it would be good to escape the dot to show OP that metacharacters need escaping.
|
0

Use a group to extract the date part:

d = re.match('^(\d{2}.\d{2}.\d{4})(.*?)$', '01.10.2018Any text..')
if d:
    print(d.group(1))
    print(d.group(2))

Group 0 is the whole string, I added a pair of parentheses in the regex around the date. This is group 1. Group 2 is the text after which is what you're after

1 Comment

That would also work for 99.10.2018Any text.. which is an invalid date...

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.