douerqu2319 2018-02-14 00:50
浏览 149
已采纳

正则表达式首次出现括号

I have a string like this:

Adress: Big Avenue 9 - National (FOREST (THE)).

And i need parse it with regex to retrieve some data like this:

  • Big Avenue 9 - National
  • FOREST (THE)

I'm using this regex:

/Adress\:\s(.*(?!\.*\)))\((.*[\(.*\)]?)\)/

But they giveme this result:

  • Big Avenue 9 - National (FOREST
  • THE)

I've searching lot's information, but I don't know how can I do it. Thank you so much for your help.

  • 写回答

2条回答 默认 最新

  • duanba5777 2018-02-14 01:08
    关注

    You may use

    '~Adress:\s*(.*?)\s*\((.*)\)~'
    

    See the PHP regex demo, results

    enter image description here

    Details

    • Adress: - a literal substring
    • \s* - 0+ whitespaces
    • (.*?) - Group 1: any 0+ chars other than line break chars, as few as possible
    • \s* - 0+ whitespaces
    • \( - a ( char
    • (.*) - Group 2: any 0+ chars other than line break chars, as many as possible
    • \) - a ) char

    See a PHP demo:

    $re = '/Adress:\s*(.*?)\s*\((.*)\)/';
    $str = 'Adress: Big Avenue 9 - National (FOREST (THE)).';
    if (preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0)) {
        print_r($matches);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部