Skip to content

Python coding : get string between two characters #21422

Discussion options

You must be logged in to vote

Take a look at regular expressions (the re module). They allow you to specify a pattern and match a string or parts of it against that pattern:

>>> import re
>>> url = 'https://partners.doctolib.fr/vaccination-covid-19/rennes/centre-de-vaccination-covid-19-rennes-school-of-business-rennes?pid=practice-205813&enable_cookies_consent=1'
>>> r = re.compile(r'/([^/\?]+)\?')
>>> m = r.search(url)
>>> m.group(1)
'centre-de-vaccination-covid-19-rennes-school-of-business-rennes'

The exact pattern depends on what exactly you’re looking for, the one I wrote searches for a /, a group of one or more characters that aren’t / or ?, and finally a ?. The group is defined by parentheses, and is helpful be…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants