[Tutor] logical oprators and REs

Kathrin Kirsch vishnja@web.de
Tue, 22 May 2001 13:11:07 +0200


Hi List,

being a newbie to programming (well, I worked myself through Ian van
Laninghams tutorial...) I'm just stucked with  the problem translating the
logical operators "AND" , "OR" and "NOT" into regular expressions.
The result shouldt be something like a search engine: the user shouldt be
able to combine those operators.. and this is where I'm stucked.
Untill now the code looks like:

"def suchwas(spr1, spr2, such):
    done=0

    if done == 0:
        such1 = string.split (such, "OR")
        if len(such1) > 1:
            done = 1
            if ganzeswort== 'ja':
# ganzeswort and grossklein are radiobuttons (options, whether to match
whole

# words and capitals)
                such = '(\\b' + such1[0] + '\\b|\\b' + such1[1] + '\\b)'
            else:
                such = string.replace(such, ' OR ', '|')

    if done == 0:
        such2 = string.split (such, "AND")
        if len(such2) > 1:
            done = 1
            if ganzeswort == 'ja':
                such = '((\\b' + such2[0] + '\\b.*\\b' + such2[1] +
'\\b)|(\\b' + such2[1] + '\\b.*' + '\\b' + such2[0] + '\\b))'
            else:
                such = '((' + such2[0] + '.*' + such2[1] + ')|(' + such2[1]
+ '.*' + such2[0] + '))'

    if done == 0:
        if ganzeswort == 'ja':
            such = '\\b' + such + '\\b'
        else:
            such = such


    if grossklein == 'nein':
        muster = re.compile(such, re.L | re.I)
    else:
        muster = re.compile(such, re.L)

    resultat  = []
"

So, how "AND" and "OR"(and "NOT") couldt be combined in one search?
I heard, that in Perl exists a parser like Yacc. Is there something like
that for Python? Or : are there solutions for that problem already?

Kathrin.