我试图在Python中使用RegEx来解析函数定义而忽略其他.我一直遇到问题.RegEx是否适合在这里使用?
即
def foo():
print bar
-- Matches --
a = 2
def foo():
print bar
-- Doesn't match as there's code above the def --
def foo():
print bar
a = 2
-- Doesn't match as there's code below the def --
Run Code Online (Sandbox Code Playgroud)
我正在尝试解析的字符串示例是"def isPalindrome(x):\n return x == x[::-1]".但实际上可能包含def本身之上或之下的行.
我必须使用什么RegEx表达式才能实现这一目标?