regex (?!..) problem

Wolfgang Rohdewald wolfgang at rohdewald.de
Mon Oct 5 03:05:28 EDT 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
differently:

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
complificate things.

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.
http://websvn.kde.org/trunk/playground/games/kmj/src/predefined.py?view=markup
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:
http://websvn.kde.org/trunk/playground/games/doc/kmj/index.docbook?revision=1030476&view=markup
(get HTML files with "meinproc index.docbook") 

-- 
Wolfgang



More information about the Python-list mailing list