a regual expression problem
arnodel at googlemail.com
Sun Nov 30 11:19:37 CET 2008
Arnaud Delobelle <arnodel at googlemail.com> writes:
> lookon <areyoulookon at gmail.com> writes:
>> 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).split('?').split('.')
> ['aaa', 'jpg']
I forgot and re solution!
>>> m=re.search(r'[^/?]*(?=\?|$)', url)
Use split('.') as above if you want name and extension separately.
You could also use something like this:
But it won't work so easily if there is no '?' in the string.
More information about the Python-list