2

I have this string /1B5DB40?full and I want to convert it to 1B5DB40.

I need to remove the ?full and the front /

My site won't always have ?full at the end so I need something that will still work even if the ?full is not there.

Thanks and hopefully this isn't too confusing to get some help :)

EDIT:
I know I could slice at 0 and 8 or whatever, but the 1B5DB40 could be longer or shorter. For example it could be /1B5DB4000?full or /1B5

falsetru
  • 357,413
  • 63
  • 732
  • 636
Brandy
  • 544
  • 2
  • 11
  • 30

5 Answers5

2

Using str.lstrip (to remove leading /) and str.split (to remove optinal part after ?):

>>> '/1B5DB40?full'.lstrip('/').split('?')[0]
'1B5DB40'

>>> '/1B5DB40'.lstrip('/').split('?')[0]
'1B5DB40'

or using urllib.parse.urlparse:

>>> import urllib.parse
>>> urllib.parse.urlparse('/1B5DB40?full').path.lstrip('/')
'1B5DB40'
>>> urllib.parse.urlparse('/1B5DB40').path.lstrip('/')
'1B5DB40'
falsetru
  • 357,413
  • 63
  • 732
  • 636
2

You can use lstrip and rstrip:

>>> data.lstrip('/').rstrip('?full')
'1B5DB40'

This only works as long as you don't have the characters f, u, l, ?, / in the part that you want to extract.

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
1

You can use regular expressions:

>>> import re

>>> extract = re.compile('/?(.*?)\?full')
>>> print extract.search('/1B5DB40?full').group(1)
1B5DB40
>>> print extract.search('/1Buuuuu?full').group(1)
1Buuuuu
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
1

What about regular expressions?

import re
re.search(r'/(?P<your_site>[^\?]+)', '/1B5DB40?full').group('your_site')

In this case it matches everything that is between '/' and '?', but you can change it to your specific requirements

Mr. E
  • 2,070
  • 11
  • 23
1
>>> '/1B5DB40?full'split('/')[1].split('?')[0]
'1B5DB40'
>>> '/1B5'split('/')[1].split('?')[0]
'1B5'
>>> '/1B5DB40000?full'split('/')[1].split('?')[0]
'1B5DB40000'

Split will simply return a single element list containing the original string if the separator is not found.

SiHa
  • 7,830
  • 13
  • 34
  • 43