Raja G
Raja G

Reputation: 6633

python3: regex need to character to match but dont want in output

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.

Upvotes: 1

Views: 30

Answers (2)

anubhava
anubhava

Reputation: 784998

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'
>>> 

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

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.

Upvotes: 3

Related Questions