php - 提取带参数的函数调用
问题描述
我有一个翻译功能,想用正则表达式列出所有带有命名空间的翻译。
到目前为止,我想出的最好的事情是:
translate[\s]*\((?|'([^']*?(?:\\'|)[^']*?)'|"([^"]*?(?:\\"|)[^"]*?)")(?:|[, ]*(?:'|")(.*?)(?:"|'))\)
但是,([^']*?(?:\\'|)[^']*?)
对于我的 regEx 技能来说,这部分太难了。有什么帮助吗?
找不到任何匹配排除序列的方法\'
。
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*\((?|'((?:[^']*?(?:\\')[^']*?)*)'|"((?:[^"]*?(?:\\"|)[^"]*?)*)")(?:|[, ]*(?:'|")(.*?)(?:"|'))\)
推荐阅读
- f# - Fable 中是否有任何优化通道可用?
- java - Java使用JInput控制器,提高性能
- django - 使用 cripsy 修改 django 表单中的 .form-group 属性
- javascript - 在移动 Safari 中打开虚拟键盘时如何修复视口?
- python - 修复了 json.dumps 上的 python for 循环不打印提供的完整列表
- c++ - C++在cpp中使用头文件中的引用函数?
- c# - OpenIddict 授权请求未处理
- javascript - Javascript列表循环/递归创建对象
- go - 在 Go 中使用单个案例的 select 语句的目的?
- php - 贝宝捐赠代码点火器