regex (?!..) problem
wolfgang at rohdewald.de
Mon Oct 5 09:05:28 CEST 2009
On Monday 05 October 2009, Stefan Behnel wrote:
> Wolfgang Rohdewald wrote:
> > I want to match a string only if a word (C1 in this example)
> > appears at most once in it.
> def match(s):
> if s.count("C1") > 1:
> return None
> return s
> If this doesn't fit your requirements, you may want to provide some
> more details.
Well - the details are simple and already given: I need re.search
to either return None or a match. But I will try to state it
I have a string representing the results for a player of a board
game (Mah Jongg - not the solitaire but the real one, played by
4 players), and I have a list of scoring rules. Those rules
can be modified by the user, he can also add new rules. Mah Jongg
is played with very different rulesets worldwide.
The rules are written as regular expressions. Since what they
do varies greatly I do not want do treat some of them in a special
way. That would theoretically be possible but it would really
For each rule I simply need to check whether it applies or not.
I do that by calling re.search(rule, gamestring) and by checking
the result against None.
Here you can look at all rules I currently have.
The rule I want to rewrite is called "Robbing the Kong". Of
course it is more complicated than my example with C1.
Here you can find the documentation for the gamestring:
(get HTML files with "meinproc index.docbook")
More information about the Python-list