我有这个字符串和模式:
s = '''double factorial ( double n ) { if ( n == 0 ) { return 1 ; } if ( n == 1 ) { return factorial(n - 2 + 1) ; } return n * factorial ( n - 1 ) ; }'''
l = re.findall(r"{ .*(factorials*(.*))", s)
目的是匹配所有Fn只调用factorial(args)部分。我如何修改上面的列表'l'返回所有相关的匹配?
('l'当前是['factorial (n - 1)'],这是findall返回后的最后一次匹配)
使用
bfactorials*([^()]*)
参见正则表达式证明。
--------------------------------------------------------------------------------
b the boundary between a word char (w) and
something that is not a word char
--------------------------------------------------------------------------------
factorial 'factorial'
--------------------------------------------------------------------------------
s* whitespace (n, r, t, f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( '('
--------------------------------------------------------------------------------
[^()]* any character except: '(', ')' (0 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
) ')'