a regual expression problem

A

Arnaud Delobelle

Arnaud Delobelle said:
lookon said:
I have a url of image, and I want to get the filename and extension of
the image. How to write in python?

for example, the url is http://a.b.com/aaa.jpg?version=1.1

how can I get aaa and jpg by python?

Without res:
url=" http://a.b.com/aaa.jpg?version=1.1"
url.rsplit('/', 1)[1].split('?')[0].split('.')
['aaa', 'jpg']

I forgot and re solution!
m=re.search(r'[^/?]*(?=\?|$)', url)
m.group()
'aaa.jpg'

Use split('.') as above if you want name and extension separately.

You could also use something like this:
url[url.rindex('/')+1:url.index('?')]

But it won't work so easily if there is no '?' in the string.
 
J

Jon Clements

I have a url of image, and I want to get the filename and extension of
the image. How to write in python?

for example, the url ishttp://a.b.com/aaa.jpg?version=1.1

how can I get aaa and jpg by python?

Something like...
from urlparse import urlsplit
from os.path import splitext
splitext(urlsplit('http://a.b.com/aaa.jpg?version=1.1').path[1:])
('aaa', '.jpg')

....and then it should be fairly flexible instead of having to change
splitting etc..

hth

Jon.
 
R

Roy Smith

lookon said:
I have a url of image, and I want to get the filename and extension of
the image. How to write in python?

for example, the url is http://a.b.com/aaa.jpg?version=1.1

how can I get aaa and jpg by python?

Despite the subject line, this is *not* a regex problem. The right tool is
Python's built-in urlparse module, which understands the full syntax of
urls. Trying to roll your own from scratch using split() or regex is the
wrong solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top