regex - 如何使用正则表达式获取字符串中的方法/函数

标签 regex pcre

我试图从字符串中的函数获取参数。

参数可能包含:

示例

占位符:

{{request.expires_in}}
//can match regex: \{\{[a-zA-z0-9\.\-\_]\}\}

函数

@func_compare('string1','string2',1234)

其他:

dERzUxOfnj49g/kCGLR3vhzBOTLwEMgrpa1/MCBpXQR2NIFV1yjraGVZLkujG63J0joj+TvNocjpJSQq2TpPRzLfCSZADcjmbkBkphIpsT8=
//Any string except brackets

案例

下面是我使用的示例案例。

内容:

@func_compare('string1',@func_test(2),1234),'Y-m-d H:i:s',@func_strtotime({{request.expires_in}} - 300)

正则表达式使用:

(?<=@func_compare\().*[^\(](?=\))

我预计会得到

'string1',@func_test(2),1234

但是现在从正则表达式匹配的是

'string1',@func_test(2),1234),'Y-m-d H:i:s',@func_strtotime({{request.expires_in}} - 300

任何人都知道如何获取 @func_strtotime 括号之间的参数。我将不胜感激任何回应。

最佳答案

请您尝试一下:

(?<=@func_compare\().*?(?:\(.*?\).*?)?(?=\))

这对于两种情况都适用。

[正则表达式说明]

.*?(?:\(.*?\).*?)?(?=\))

.*?                       the shortest match not to overrun the next pattern
   (?:\(.*?\).*?)?        a group of substring which includes a pair of parens followed by another substring of length 0 or longer
                  (?=\))  positive lookahead for the right paren

关于regex - 如何使用正则表达式获取字符串中的方法/函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60647106/

相关文章:

regex - 在 sqlite3 中用 REGEXP 替换字符串的一部分

php - 将 block 内的 block 与 pcre 匹配

c# - 突出整个单词,省略 HTML

regex - 使用awk查找包含最长重复单词的域名

Javascript 函数从网页查找电子邮件地址

python-3.x - 没有安装候选者时如何将libpcre3-dev添加到docker image python3.8-slim

php - 正则表达式 - Unicode 属性引用和示例

javascript - 获取顺序中所有可能的子串

c# - 字符串中任何位置都不允许使用单词和字符的正则表达式,例如 ({}[]^)

php - 关于回溯控制动词的信息