I have a url that contains "#/mail_detail?mail_id=16e7e90049d734ed&_k=iddh5c"
and from it I would like to store the value of mail_id
till &
.
Is there a simple way to do it using python?
I have a url that contains "#/mail_detail?mail_id=16e7e90049d734ed&_k=iddh5c"
and from it I would like to store the value of mail_id
till &
.
Is there a simple way to do it using python?
You may use regular expressions (re
library) to find all characters in between the string
mail_id
and the &
character
import re
url = '#/mail_detail?mail_id=16e7e90049d734ed&_k=iddh5c'
print(re.findall(r'mail_id=(.*&)', url))
.*
matches any no. of characters except a .
You can use re library to do the extraction easily. Assuming your url data type is a string here is the simple code to do so:
import re
url = "#/mail_detail?mail_id=16e7e90049d734ed&_k=iddh5c"
result = re.search("mail_id=(.*)&_k", url)
print(result.group(1))
Please note that using the command:
result = re.search("_id=(.*)&_k", url)
will yield the same result as well, but in case your string contains more than one such instances where "_id" appears, then it can be an ambiguous situation, where the output may be different. To ensure the 100 percent correct results, you should preferably include the more certain string keywords, around which you want to extract the text.