3

So yeah, the title is pretty weird but I have no other idea how to describe my problem properly. Whatever... lets get to the problem.

Job to get done

My boss wants a function that read all functions of a python file and return a DataTable containing the found functions. This function should be written in IronPython (Python which actually uses C# libraries).

The Problem

I am relatively new to Python and I have no idea what this language is capable of, so I started to write my function and yeah it works pretty well, except one weird problem. I wrote a regular expression to find the functions and to test it I downloaded a RegEx Tester. The Regex Tester showed the results I wanted: Group 1 - The function name, Group 2 - The functions parameter and Group 3 - the content of the function.

For some magical reasons, it doesn't work when it goes to live testing. And with doesn't work I mean, Group 3 has actually no output. After testing the expression with another (online) RegEx Tester, it showed me, that Group 3 has actually not the content of the function, it only has a small part of it, starting with a newline/return character.

In my test cases, the results of Group 3 where all the same, starting with a newline/return character and ended with the functions return (e.g. return objDic).

Question: What the hell is going wrong there? I have no idea what is wrong on my RegEx.

The Regex

objRegex = Regex(r"(?i)def[\s]+([\w]+)\(([\, [\w]+)\)(?:[\:{1}]\s*)([\n].*(?!\ndef[\s]+))+")

The Data

def test_function(some_parameter):
    try:
        some_cool_code_goes_here()
        return obj
    except Exception as ex:
        DetailsBox.Show(ex)

def another_cool_function(another_parameter):
    try:
        what_you_want()
        return obj
    except Exception as ex:
        DetailsBox.Show(ex)

The Result

Match: def test_function(some_parameter):...
Position: ..
Length: ..
Group 1: test_function
Group 2: some_parameter
Group 3: (newline/return character) return obj

But Group 3 should be:

    try:
        some_cool_code_goes_here()
        return obj
    except Exception as ex:
        DetailsBox.Show(ex)

I hope you can help me :3 Thank you guys!

4

2 に答える 2

2

@Hamzaはコメントで、正規表現にいくつかの問題があると述べましたが、それらは不必要に複雑であると思います.bodyと一致しない理由は、.特別なメタ文字を新しい行に一致させていない可能性があるためです。最初のTry:ステートメントの後の最初の改行文字で停止しています。

.これを修正するには、改行文字を一致させる必要があります。これは、機能する正規表現の簡略化されたバージョンです。

(?i)def\s+(\w+)\s*\(([\, \w]+)\)(?:\s*:\s*)(.+?)(?=def|$)
于 2013-08-14T13:25:11.670 に答える
1

HamZaの迅速なサポートに感謝します (もちろん、他のすべてのヘルパーにも感謝します)、彼は実際に問題を解決しました。ほんの少しの調整が必要でした (C# で機能させるために :-)) が、主なポイントは彼から来ています。どうもありがとうございました。

私の問題の解決策:

Regex(r"(?is)def\s*(?<name>\w+)\s*\((?<parameter>[^)]+)\)\s*:\s*(?:\r?\n)+(?<body>.*?)(?=\r?\ndef|$)")
于 2013-08-14T13:36:52.447 に答える