drr47973 2014-01-22 20:22
浏览 68
已采纳

PHP Regex用于多个括号

I have a string which looks like this:

43.3433443 25.3434334), (43.32424432 25.435345345, 43.21313123, 25.32432423423), (43.123123123 25.344234234, 43.1234123123 25.23213123))

I need to extract everything that is between ( and ) as well as ( and )) with preg_match_all

I have the following regex, which works for extracting everything between ( and ) but not ( and ))

preg_match_all('/\), \(([A-Za-z0-9., ]+?)\)/', $data[$x], $inner);

I've tried using:

preg_match_all('/\), \(([A-Za-z0-9., ]+?)\)|\), \(([A-Za-z0-9., ]+?)\)\)/', $data[$x], $inner);

and:

preg_match_all('/(?=\), \(([A-Za-z0-9., ]+?)\))(?=\), \(([A-Za-z0-9., ]+?)\)\))/', $data[$x], $inner);

But none of them have worked. How would I go about to include both cases?

Thank you.

UPDATE:

I forgot to mention, the string contains numbers within multiple starting and ending parentheses that I don't want, i.e. two parantheses ---> (( numbers here ))) <---3 parantheses

  • 写回答

2条回答 默认 最新

  • doushua7737 2014-01-22 20:34
    关注

    Use following regex:

    ([^()]+?)\)
    

    Live demo

    PHP code:

    preg_match_all('/([^()]+?)\)/', $data[$x], $inner);
    

    Regex for your last update:

    ([^()]+?)\)(?![)]{2,})
    

    Live demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭