FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

Python Expression Evaluator Version 2




I have extended my Python Expression Evaluator a bit, adding the main feature to support regular expressions. Regular expressions need to be typed in between slashes and need some input text to be applied to, that's why now a second input field has been added to the user interface.

Regular expressions are very well supported in Python ( like in Perl ), thus not much additional code is needed to support those:
Code:

...
    if expr[0] == "/":
        m = re.match("\/(.*)\/",expr)
        if m:
            expr2 = m.group(1)
            m = re.match(expr2,input)
            if m:
                for i in range(len(input)):
                    if i in range(m.start(0),m.end(0)): char = "<strong>%s</strong>" % input[i]
                    else: char = "%s" % input[i]
                    for j in range(1,len(m.groups())+1):
                        if i in range(m.start(j),m.end(j)): char = "<span class='highlighted'>%s</span>" % char
                    output += char   
               
    else:
...

The Python code above first checks whether input contains a regular expression ( starting with a forward slash ). The first regular expression evaluation (re.match(...); re is the name of the Python module for regular expressions to be imported at the beginning of the program ) is to get the regular expression itself in between the two slashes, the second regular expression evaluation actually evaluates that regular expression.
m is the name of the object returned by the evaluation, having some useful attributes:

    m.group(0) contains the matching part of the input
    m.start(0) contains the starting position of the matching part of the input
    m.endt(0) contains the ending position of the matching part of the input
    m.groups() is a list of groups defined in the regular expression to extract part of the input; defined in form of round brackets within the regular expression
    m.group(n) with n > 0 is content of group n
    m.start(n) with n > 0 is the starting position of group n
    m.end(n) with n > 0 is the ending position of group n

Those useful object attributes help to do what I attempt to do with that code: highlight those characters in my input string being in the overall match or in any of the groups: the first case is indicated by a bold font, being contained in a group by yellow background color.
Thus my for loop
Code:

for i in range(len(input)):

iterates over the input provided character by character.Then I analyze whether that character is contained in the overall match to put some HTML "strong" tags around it. Then I analyze for each group returned ...
Code:

for j in range(1,len(m.groups())+1):

... whether the character is contained in a group and give it a yellow background in that case.

To do this I use a class called "highlighted" defined in my CSS file for this little application:
Code:

.highlighted
{
    background: yellow;   
}

And there it is: version 2 of my Python Expression Evaluator supporting regular expressions.



1 blog comments below

Nice alternative here: http://ideone.com
And it knows 40 other programming languages !
amagard on Fri Apr 05, 2013 3:11 pm



FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.