2

I have the following string:

EUID=000000;GTIME=503551082,+0;KEY=034534D9D1644C0B27404283D04B78B9B31BA143D80ABDF8781FE45D8246B92;IV=9B3112343D80ABDF8781FE45D8246B92;GPOS=+00000.0000,+00000.0000;

I would like to extract each value between an = and a ;.

I've tried the following: d_block = (the string above)

import re    
re_euid = re.search('EUID=(.*);', d_block)
    euid = re_euid.group(1)

    re_gtime = re.search('GTIME=(.*),', d_block)
    gtime = re_gtime.group(1)

    re_tzone = re.search(',(.*);', gtime)
    tzone = re_tzone.group(1)

    re_key = re.search('KEY=(.*);', d_block)
    key = re_key.group(1).upper()

    re_iv = re.search('IV=(.*);', d_block)
    iv = re_iv.group(1)

    re_gpos = re.search('GPOS=(.*);', d_block)
    gpos = re_gtime.group(1)

For some reason...Each value is returning more than it should. It's not stopping at the ; as I'd like.

Example of current data returned for euid:

000000;GTIME=503551082,+0;KEY=0061ECAD9D1644C0B27404283D04B78B9B31BA143D80ABDF8781FE45D8246B92;IV=9B31BA143D80ABDF8781FE45D8246B92;GPOS=+00000.0000,+00000.0000

Which is the whole string except for the EUID=.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Kenny Powers
  • 1,254
  • 2
  • 15
  • 25
  • Briefly: you need `?`. – TigerhawkT3 Dec 17 '15 at 01:36
  • 1
    You need `*` to be non-greedy. Try a `?` after the `.*` or change it to accept any character except `;` – Alex W Dec 17 '15 at 01:37
  • Just 2 cents, (1) why use regex here if you can just split by `;` as you are treating every part different anyway (2) i think the point and efficiency of the regex gets lost if you keep applying 6 different patterns to the same string if you could do this with 1 pattern. I honestly would go with (1) :) – enpenax Dec 17 '15 at 01:56
  • 2
    And I'd just do `res = dict(re.findall(r'([A-Z]+)=(.*?);', d_block))` and then properly parse each object (such as turning `GTIME`'s value into a `datetime` object). – TigerhawkT3 Dec 17 '15 at 01:56
  • Can you add an example that would work for this that uses only one pattern? A start to finish for the findall() – Kenny Powers Dec 17 '15 at 02:03
  • 1
    That's exactly what I wrote above. It even creates a tidy dictionary for you. Post-processing the matching strings is up to you. – TigerhawkT3 Dec 17 '15 at 02:23

0 Answers0