提取带参数的函数调用

Extracting function calls with parameters

我有一个翻译功能,想用正则表达式列出所有带有命名空间的翻译。

到目前为止我想到的最好的事情是:

translate[\s]*\((?|'([^']*?(?:\'|)[^']*?)'|"([^"]*?(?:\"|)[^"]*?)")(?:|[, ]*(?:'|")(.*?)(?:"|'))\)

但是,([^']*?(?:\'|)[^']*?) 部分对我的正则表达式技能来说太难了。有帮助吗?

找不到任何方法来匹配排除序列 \'

translate('hello what\'s wrong');
translate('hello what\'s wrong');
translate('hello what\'s wrong');
translate("hello what's w: )rong");
translate("hello whats wrong");
translate('hello what\'s wrong');

translate('hello what\'s wrong', 'namespace');
translate('hell\'o what\'s wro\'ng', 'namespace');
translate("hello what\"s \" s\" wrong", 'namespace');
translate('hell(o) (wh)at\'s wrong', "namespace");
translate("hello what's wrong", "namespace");

translate  ('hello what\'s wrong');
translate("hel
lo what's wrong");

translate('hello what\'s wrong', 'namespace');
translate("hello wh
at's wrong", 'namespace');
translate('hello what\'s wrong', "namespace");
translate("hello what's wrong", "namespace");

translate[\s]*\((?|(?:')([^']*?(?:\'|)[^']*?)(?:')|(?:")([^"]*?(?:\"|)[^"]*?)(?:"))(?:|[, ]*(?:'|")(.*?)(?:"|'))\)

为了更轻松地调试不工作的地方https://regex101.com/r/8Jzso3/4

PS:我可能已经把所有这些组复杂化了。

要匹配所有测试字符串,我想你需要做的就是*重复开始'和结束'之间的组,或者开始" 和结尾的 "(匹配非转义引号直到单个转义 \'\" 的组)。将重复组变成非捕获组,并在重复组周围使用另一个组来捕获参数字符串中外引号之间的所有内容。

您还可以将 translate[\s]* 简化为 translate\s*,因为该字符集中只有一个字符。

translate\s*\((?|'((?:[^']*?(?:\')[^']*?)*)'|"((?:[^"]*?(?:\"|)[^"]*?)*)")(?:|[, ]*(?:'|")(.*?)(?:"|'))\)

https://regex101.com/r/8Jzso3/6