2

I'm about to write a regex to extract substrings. the string is:

ASP.NET_SessionId=frffcjcarie4dhxouz5yklwu;+BIGipServercapitaliq-ssl=3617221783.36895.0000;+ObSSOCookie=wkyQfn2Cyx2%2f7kSj4zBB886WaLs92Ord9FSf64c%2byHFOBwgEP4f3UmorDj051suQwRXAKEwBtYVKRYJuUGh2YNZtAj2%2bNp8asLIT9xQPqVktEAzkl3jNIv8MyWFsoFPDtm%2fTm1FeaCP%2bGTk9Oa%2fCNA0Hmy847qK2qo7%2bbziV%2bjeClbkGjAX3pgcPzfs%2bQp7p9BSjP1xJqUaUKwJ2%2flIgzZL5Ma%2bnJK8j%2b732ixNyIDNDGo7uIF%2b;+machineIdCookie=866873600;+userLoggedIn=jga;sdgjefdfdfs

I want to extract a substring beginning with ObSSOCookie=....; and ending just before the userLoggedIn.

I set my regex pattern

pattern = "ObSSOCookie=.*;" 

But it continues to extract until the last semicolon (which includes the +machineIdCookie=866873600), rather than the first semicolon, which is what I want.

Is there a way to just extract up to the first semicolon? And I can't just use split by ";" cause this regex is actually to be used in a Logstash configuration file and there's no way to use python-style coding there...

divibisan
  • 11,659
  • 11
  • 40
  • 58
JudyJiang
  • 2,207
  • 6
  • 27
  • 47

2 Answers2

2

You want to make your regex non-greedy

Instead of using this

*  - zero or more

Use this

*? - zero or more (non-greedy)

Here's your expression (demo).

ObSSOCookie=(.*?;)

This is a general technique, also described in this answer.

Community
  • 1
  • 1
Agostino
  • 2,723
  • 9
  • 48
  • 65
0

Why not just grab anything except the next ; like this (demo)

 ObSSOCookie=([^;]*)


>>> import re
>>> data = 'ASP.NET_SessionId=frffcjcarie4dhxouz5yklwu;+BIGipServercapitaliq-ssl=3617221783.36895.0000;+ObSSOCookie=wkyQfn2Cyx2%2f7kSj4zBB886WaLs92Ord9FSf64c%2byHFOBwgEP4f3UmorDj051suQwRXAKEwBtYVKRYJuUGh2YNZtAj2%2bNp8asLIT9xQPqVktEAzkl3jNIv8MyWFsoFPDtm%2fTm1FeaCP%2bGTk9Oa%2fCNA0Hmy847qK2qo7%2bbziV%2bjeClbkGjAX3pgcPzfs%2bQp7p9BSjP1xJqUaUKwJ2%2flIgzZL5Ma%2bnJK8j%2b732ixNyIDNDGo7uIF%2b;+machineIdCookie=866873600;+userLoggedIn=jga;sdgjefdfdfs'
>>> p = re.compile('ObSSOCookie=([^;]*)')
>>> m = p.search(data)
>>> m.group(1)
'wkyQfn2Cyx2%2f7kSj4zBB886WaLs92Ord9FSf64c%2byHFOBwgEP4f3UmorDj051suQwRXAKEwBtYVKRYJuUGh2YNZtAj2%2bNp8asLIT9xQPqVktEAzkl3jNIv8MyWFsoFPDtm%2fTm1FeaCP%2bGTk9Oa%2fCNA0Hmy847qK2qo7%2bbziV%2bjeClbkGjAX3pgcPzfs%2bQp7p9BSjP1xJqUaUKwJ2%2flIgzZL5Ma%2bnJK8j%2b732ixNyIDNDGo7uIF%2b'
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124