New Noob
New Noob

Reputation: 23

Python Regex Problems and grouping

I'm trying to parse data from a text file that has lines like:

On 1-1-16 1:48 Bob used: 187
On 1-5-16 2:50 Bob used: 2

I want to print only the time and the number used, so it would look like:

1-1-16, 1:48, 187
1-5-16, 2:50, 2

I'm using this regex:

print(re.search(r"On ([0-9,-, ]+)Bob used ([0-9\.]+)", line.strip()))

I get results that say <_sre.SRE_Match object; span=(23, 26), match='Bob used: 187'>

I tried using .group() but it give the error "'NoneType' object has no attribute 'group'" I also noticed its only finding the second grouping (the number) and not the first (the date and time).

How can this be fixed?

Upvotes: 2

Views: 49

Answers (3)

yoyoyoyo123
yoyoyoyo123

Reputation: 2472

I'm pretty new to regular expressions myself however I came up with this

import re
source = "On 1-1-16 1:48 Bob used: 187\nOn 1-5-16 2:50 Bob used: 2" 
x=re.finditer('([0-9]-)+[0-9]+',source)
y=re.finditer('[0-9]+:[0-9]+',source)
z=re.finditer(': [0-9]*',source)
L = []
for i,j,k in zip(x,y,z):
    L.append((i.group(), j.group(), k.group().replace(': ', '') ))

print(L)

output

[('1-1-16', '1:48', '187'), ('1-5-16', '2:50', '2')]

Upvotes: 1

ShadowRanger
ShadowRanger

Reputation: 155363

You didn't give enough information on how you're using it, but since you're getting a Match object back, it shouldn't be None when you call .group() unless you're failing to store the result to the correct place. Most likely you are processing many lines, some of which match, and some of which don't, and you're not checking whether you matched before accessing groups.

Your code should always verify it got a Match before working with it further; make sure your test is structured like:

match = re.search(r"On ([0-9,-, ]+)Bob used ([0-9\.]+)", line.strip())
if match is not None:
    ... do stuff with match.group() here ...
... but not here ...

Upvotes: 1

alecxe
alecxe

Reputation: 473863

You are missing the : after the Bob used and you need are more precise expression for the date part - for instance, \d+-\d+-\d+ \d+:\d+:

>>> s = 'On 1-1-16 1:48 Bob used: 187 On 1-5-16 2:50 Bob used: 2'
>>> re.search(r"On (\d+-\d+-\d+ \d+:\d+) Bob used: ([0-9\.]+)", s).groups()
('1-1-16 1:48', '187')

Upvotes: 1

Related Questions