Here's the problem.
I'm using pythons re-functions.

I'm supposed to make a re-function to check if a string
ends with 3 numbers(or more) before the extension.(e.g. 123.txt, 93821.ini)

Here's my code that's supposed to do the magic

re.search('[0-9][0-9][0-9]\.','1234.txt')

now, this doesn't work out. And I have no clue why not.
I reccon it's the "\." that doesn't work properly since;

re.search('[0-9][0-9][0-9]_','1234_txt')

works perfectly.

Recommended Answers

All 4 Replies

It seems to work here...

import re

print re.findall('[0-9][0-9][0-9]\.', '1234_.txt')
print
print re.findall('[0-9][0-9][0-9]\.', '1234.txt')

Can you explain what's not working.

Cheers and Happy coding

Thanks for the reply

oh, I thought that "." without the \ meant any symbol

so if i wrote:

re.search('[0-9][0-9][0-9]\.','1234atxt')

would work too. But now I see that it doesn't. Why is that? :S

You are write, just not implementing good.

Because as you say, if you don't escape the '.' it will match any simbol, and so...

re.search('[0-9][0-9][0-9].','1234atxt')

will work. But as you 'escaped' it using the '\' it matchs the '.'.

Cheers and happy coding

Great! Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.