[plug] Regex help

Carl Gherardi carl.gherardi at gmail.com
Wed Jun 29 12:13:28 WST 2011


Hey Guys,

I'm having a bit hard time spotting the issue with a regex of
reasonable complexity. The following is a simplified version of the
problem, the idea being to split a section of a file into sections for
later parsing.

import re
import pprint
string="""
predeal content
Player5: posts small blind $0.25
Hero: posts big blind $0.50
*** DEALING HANDS ***
deal content
Dealt to Hero [2s 7d 4d 6d 8s]
Player3: raises $1.50 to $2
Player5: folds
Hero: calls $1.50
Hero: stands pat on [2s 7d 4d 6d 8s]
Player3: discards 1 card
drawone content
*** SHOW DOWN ***
Hero: shows [2s 7d 4d 6d 8s] (Lo: 8,7,6,4,2)
Player3: mucks hand
Hero collected $4.05 from pot
"""
rexx = re.search(r"(?P<PREDEAL>.+(?=\*\*\* DEALING HANDS \*\*\*)|.+)"
                 r"(\*\*\* DEALING HANDS \*\*\*(?P<DEAL>.+(?=^(.+?:
stands pat on|.+?: discards))|.+))?"
                 r"((?=^(.+?: stands pat on|.+?:
discards))(?P<DRAWONE>.+))?", string,re.DOTALL|re.MULTILINE)
pprint.pprint(rexx.groupdict())

Gives:
{
'PREDEAL': '\npredeal content\nPlayer5: posts small blind $0.25\nHero:
posts big blind $0.50\n'
'DEAL': '\ndeal content\nDealt to Hero [2s 7d 4d 6d 8s]\nPlayer3:
raises $1.50 to $2\nPlayer5: folds\nHero: calls $1.50\nHero: stands
pat on [2s 7d 4d 6d 8s]\n',
'DRAWONE': 'Player3: discards 1 card\ndrawone content\n*** SHOW DOWN
***\nHero: shows [2s 7d 4d 6d 8s] (Lo: 8,7,6,4,2)\nPlayer3: mucks
hand\nHero collected $4.05 from pot\n',
}

Which is close to what I want, but the section 'Hero: stands pat on
[2s 7d 4d 6d 8s]\n' currently in 'DEAL' should be in DRAWONE

Requirements
- There may not be a DRAWONE section at all, and if so everything
after DEALING HANDS should be in 'DEAL'
- The split between DEAL and DRAWONE should be on the first 'stands
pat' or 'discards', with the entire line that is matched going into
DRAWONE.

Any hints welcome.

Carl G



More information about the plug mailing list