How to get particular text using regex

Question:

import re
cookie_jar = "<RequestsCookieJar[<Cookie JSESSIONID=EA47DC319D1977863BBEC8E4AFB408DF.node60877 for tah.appiancloud.com/suite>, <Cookie SCS_REMEMBER_ME_COOKIE=jA0EAwMC3Bc7KwH4uMsB0kUBOqr2TjQT9LIqoOmfUaCFiPtA7JqG9A8lihrCrKyg6yK2EYEqkHvCfEQwVTcKP9CYYFtwGkG7rGDH8UY6IPhJPKa2tsg for tah.appiancloud.com/suite>, <Cookie SPRING_SECURITY_REMEMBER_ME_COOKIE=TExNTXNyJTJGQjBqVTN6OWFZQ0xSV1RuOVhVcEhtczdhNlFXd0swJTJCd2VGOEElM0Q6TGRQU0MlMkZHRVgyYVhKZldBTVQwQWdQVTMzWTA5VENvaUY0NWxBdlhiYSUyRlklM0Q for tah.appiancloud.com/suite>, <Cookie __appianCsrfToken=d815e694-2118-4b3e-92c7-6b750b9557d2 for tah.appiancloud.com/suite>, <Cookie __appianMultipartCsrfToken=f80ce677-9adf-409e-a755-89105f51437e for tah.appiancloud.com/suite>]>
"
print(re.findall("__appianCsrfTokens*=s*([^;]+);?",cookie_jar))

Required output: d815e694-2118-4b3e-92c7-6b750b9557d2

Is there any solution to get __appianCsrfToken only the value

Asked By: Ajay Kk

||

Answers:

regex description https://regex101.com/r/b42RMQ/1

import re
cookie_jar = "<RequestsCookieJar[<Cookie JSESSIONID=EA47DC319D1977863BBEC8E4AFB408DF.node60877 for tah.appiancloud.com/suite>, <Cookie SCS_REMEMBER_ME_COOKIE=jA0EAwMC3Bc7KwH4uMsB0kUBOqr2TjQT9LIqoOmfUaCFiPtA7JqG9A8lihrCrKyg6yK2EYEqkHvCfEQwVTcKP9CYYFtwGkG7rGDH8UY6IPhJPKa2tsg for tah.appiancloud.com/suite>, <Cookie SPRING_SECURITY_REMEMBER_ME_COOKIE=TExNTXNyJTJGQjBqVTN6OWFZQ0xSV1RuOVhVcEhtczdhNlFXd0swJTJCd2VGOEElM0Q6TGRQU0MlMkZHRVgyYVhKZldBTVQwQWdQVTMzWTA5VENvaUY0NWxBdlhiYSUyRlklM0Q for tah.appiancloud.com/suite>, <Cookie __appianCsrfToken=d815e694-2118-4b3e-92c7-6b750b9557d2 for tah.appiancloud.com/suite>, <Cookie __appianMultipartCsrfToken=f80ce677-9adf-409e-a755-89105f51437e for tah.appiancloud.com/suite>]>"
print(re.findall(r"__appianCsrfTokens*=([^s]+)",cookie_jar)) #['d815e694-2118-4b3e-92c7-6b750b9557d2']
Answered By: Epsi95

You need to replace your regex by __appianCsrfTokens*=s*([^s]+)s

So your final code is :

import re
cookie_jar = "<RequestsCookieJar[<Cookie JSESSIONID=EA47DC319D1977863BBEC8E4AFB408DF.node60877 for tah.appiancloud.com/suite>, <Cookie SCS_REMEMBER_ME_COOKIE=jA0EAwMC3Bc7KwH4uMsB0kUBOqr2TjQT9LIqoOmfUaCFiPtA7JqG9A8lihrCrKyg6yK2EYEqkHvCfEQwVTcKP9CYYFtwGkG7rGDH8UY6IPhJPKa2tsg for tah.appiancloud.com/suite>, <Cookie SPRING_SECURITY_REMEMBER_ME_COOKIE=TExNTXNyJTJGQjBqVTN6OWFZQ0xSV1RuOVhVcEhtczdhNlFXd0swJTJCd2VGOEElM0Q6TGRQU0MlMkZHRVgyYVhKZldBTVQwQWdQVTMzWTA5VENvaUY0NWxBdlhiYSUyRlklM0Q for tah.appiancloud.com/suite>, <Cookie __appianCsrfToken=d815e694-2118-4b3e-92c7-6b750b9557d2 for tah.appiancloud.com/suite>, <Cookie __appianMultipartCsrfToken=f80ce677-9adf-409e-a755-89105f51437e for tah.appiancloud.com/suite>]>"
result = re.findall("__appianCsrfTokens*=s*([^s]+)s",cookie_jar)
if result:
    print(result[0])

Result :

d815e694-2118-4b3e-92c7-6b750b9557d2
Answered By: NIKUNJ KOTHIYA
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.