1

I have a string named

Set-Cookie: BIGipServerApp_Pool_SSL=839518730.47873.0000; path=/

I am trying to extract 839518730.47873.0000 from it. For exact string I am fine with my regex but If I include any digit before 1st = then its all going wrong.

No Digit

>>> m=re.search('[0-9.]+','Set-Cookie: BIGipServerApp_Pool_SSL=839518730.47873.0000; path=/')
>>> m.group()
'839518730.47873.0000'

With Digit

>>> m=re.search('[0-9.]+','Set-Cookie: BIGipServerApp_Pool_SSL2=839518730.47873.0000; path=/')
>>> m.group()
'2'

Is there any way I can extract `839518730.47873.0000' only but doesnt matter what else lies in the string.

I tried

>>> m=re.search('=[0-9.]+','Set-Cookie: BIGipServerApp_Pool_SSL=839518730.47873.0000; path=/')
>>> m.group()
'=839518730.47873.0000'

As well but its starting with '=' in the output and I dont want it.

Any ideas.

Thank you.

1
  • 1
    result=re.search(r'=([0-9.]+)',input).group(1) Commented May 24, 2016 at 13:33

2 Answers 2

3

If your substring always comes after the first =, you can just use capture group with =([\d.]+) pattern:

import re
result = ""
m = re.search(r'=([0-9.]+)','Set-Cookie: BIGipServerApp_Pool_SSL2=839518730.47873.0000; path=/')
if m:
    result = m.group(1)  # Get Group 1 value only
print(result)

See the IDEONE demo

The main point is that you match anything you do not need and match and capture (with the unescaped round brackets) the part of pattern you need. The value you need is in Group 1.

Sign up to request clarification or add additional context in comments.

1 Comment

You can read more on capturing at regular-expressions.info, BTW. And this "best regex trick ever" article is also very educating.
1

You can use word boundaries:

\b[\d.]+

RegEx Demo

Or to make match more targeted use lookahead for next semi-colon after your matched text:

\b[\d.]+(?=\s*;)

RegEx Demo2

Update :

>>> m.group(0)
'839518730.47873.0000'
>>> m=re.search(r'\b[\d.]+','Set-Cookie: BIGipServerApp_Pool_SSL2=839518730.47873.0000; path=/')
>>> m.group(0)
'839518730.47873.0000'
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.