小编Str*_*ngs的帖子

如何将python函数定义(以及其他任何内容)与RegEx相匹配?

我试图在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表达式才能实现这一目标?

python regex

2
推荐指数
1
解决办法
597
查看次数

标签 统计

python ×1

regex ×1