Regex to match everything between the first and last occurrence of two distinct characters

前端 未结 3 547
闹比i
闹比i 2020-12-18 10:10

I have the following code:

$str_val = \"L(ine 1(
    L(ine 2)
    Line 3
    Line 4)\";
$regex = \'/\\(([^\\)]*?)\\)/i\';
preg_match($regex, $str_val, $match         


        
相关标签:
3条回答
  • 2020-12-18 10:24

    Just do a greedy search

    $regex = '/\(.*\)/s';
    

    If you really want to have everything between (...) use this one

    $regex = '/\((.*)\)/s';
    preg_match($regex, $str_val, $matches_arr);
    echo $matches_arr[1];
    
    0 讨论(0)
  • 2020-12-18 10:29

    Try this regular expression:

    \([^\)]*\)
    

    The first match is what you need.

    0 讨论(0)
  • 2020-12-18 10:32

    You can use this: -

    '/\((.*)\)/s'
    

    /s modifier is used to enable the dot metacharacter to match everything including a newline. And, since .* is a greedy quantifier, it will match the longest string possible. So, it will match till the last ).

    0 讨论(0)
提交回复
热议问题