开发者

Regular expression further checking

I am working with a regular expression that would check a string,Is it a function or not. My regular expression for checking that as follows:

regex=r' \w+[\ ]*\(.*?\)*'

It succefully checks whether the string contains a function or not. But it grabs normal string which contains firs barcket value,such as "test (meaning of test)".

So I have to check further that if there is a space between function name and that brackets that will not be caught as match.So I did another checking as follows:

regex2=r'\s'

It work successfully and can differentiate between "test()" and "test ()". But now I have to maintain another condition that,if there is no space after the brackets(eg. test()abcd),it will not catch it as a function.The regular expression should only treat as match when it will be like "test() abcd". But I tried using different regular expression ,unfortunately those are not working. Her one thing to mention the checking string is inserted in to a list at when it finds a match and in second step it only check the portion of the string.Example: String : This is a python function test()abcd At first it will check the string for function and when find matches with 开发者_JAVA百科function test() then send only "test()" for whether there is a gap between "test" and "()". In this last step I have to find is there any gap between "test()" and "abcd".If there is gap it will not show match as function otherwise as a normal portion of string. How should I write the regular expression for such case?

The regular expression will have to show in following cases:

1.test() abc
2.test(as) abc
3.test()

will not treat as a function if:

1.test (a)abc
2.test ()abc


(\w+\([^)]*\))(\s+|$)

Bascially you make sure it ends with either spaces or end of line.

BTW the kiki tool is very useful for debugging Python re: http://code.google.com/p/kiki-re/


regex=r'\w+\([\w,]+\)(?:\s+|$)'



I have solved the problem at first I just chexked for the string that have "()"using the regular expression:

regex = r' \w+[\ ]*\(.*?\)\w*'

Then for checking the both the space between function name and brackets,also the gap after the brackets,I used following function with regular expression:

       def testFunction(self, func):
            a=" "
            func=str(func).strip()
            if  a in func:
                return False
            else:   
                a = re.findall(r'\w+[\ ]*', func)
                j = len(a)
                if j<=1:
                    return True
                else:
                    return False

So it can now differentiate between "test() abc" and "test()abc".
Thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜