-2

I have a string showing as

text = "isLogIn=true; Max-Age=400; Path=/; Expires=Wed, 25 Sep 2019 17:46:30 GMT, value=qasde% , client=retail"

I need to extract the string showing for value, ie qasde%. I am using Python scripting

What i tried was

text = text.split('value', 1)[1]
text = text.split('client',1)[0]

It gives me the correct value, but is there any simple way to achieve this ?

wanderors
  • 2,030
  • 5
  • 21
  • 35

3 Answers3

2

https://regex101.com/r/u9gpm5/2

import re

text = 'isLogIn=true; Max-Age=400; Path=/; Expires=Wed, 25 Sep 2019 17:46:30 GMT, value=qasde% , client=retail'

m = re.search('value=(.+?) , client=', text)
if m:
    found = m.group(1)

Credits to https://stackoverflow.com/a/4667014/11063448

1

You didn't provide enough information to fully answer the question. Does value always appear before client? Does it always appear after the time and date?

Anyway - this answer, or Quicksilver's answer should work if value does always appear after date and time and before client.

However, you might also just want to search for whatever appears after value= and before ,, in which case this should work: https://regex101.com/r/a4d7Bx/1

import re

text = 'isLogIn=true; Max-Age=400; Path=/; Expires=Wed, 25 Sep 2019 17:46:30 GMT, value=qasde% , client=retail'

m = re.search('value=(.+?) ,', text)
if m:
    found = m.group(1)
    print(found)

Another way to do this by slightly editing your code:

text = 'isLogIn=true; Max-Age=400; Path=/; Expires=Wed, 25 Sep 2019 17:46:30 GMT, value=qasde% , client=retail'

text = text.split('value=', 1)[1]
text = text.split(' , client',1)[0]
print(text)

You asked for the simpler way. Arguably, both of these methods are pretty simple, so I'd opt for the faster one (unless you want the more general solution, in which case use the first option). I timed them using the following code:

import re
import cProfile

text = 'isLogIn=true; Max-Age=400; Path=/; Expires=Wed, 25 Sep 2019 17:46:30 GMT, value=qasde% , client=retail'

def option1(text):
  for i in range(10000):
    re.search('value=(.+?) ,', text)

def option2(text):
  for i in range(10000):
    temp = text.split('value=', 1)[1]
    temp = temp.split(' , client',1)[0]

cProfile.run("option1(text)")
print("\n\n\n\n\n")
cProfile.run("option2(text)")

And the first option took 0.076 seconds while the second took 0.027 seconds, so the faster option is using split.

alon-k
  • 330
  • 2
  • 11
  • Ok. In light of the code you posted above - yeah, a perhaps "simpler" way to do it is the code I posted above. I also edited my answer to include an edited version of your code that does the same thing. – alon-k Sep 24 '19 at 18:09
1

This expression might simply return that:

import re

expression = r'(?:value|client)\s*=\s*([^,\r\n]*?)(?=\s*,|$)'

string = """
isLogIn=true; Max-Age=400; Path=/; Expires=Wed, 25 Sep 2019 17:46:30 GMT, value=qasde% , client=retail

"""

print(re.findall(expression, string, re.M))

Output

['qasde%', 'retail']

If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Emma
  • 27,428
  • 11
  • 44
  • 69