exploding a string using a regular expression

前端 未结 1 1590
傲寒
傲寒 2021-01-22 16:23

I have a string as below (the letters in the example could be numbers or texts and could be either uppercase or lowercase or both. If a value is a sentence, it should be between

相关标签:
1条回答
  • 2021-01-22 16:56

    You can do the job using preg_match_all

    $string="a,b,c,(d,e,f),g,'h, i j.',k";
    
    preg_match_all('~\'[^\']++\'|\([^)]++\)|[^,]++~', $string,$result);
    print_r($result[0]);
    

    Explanation:

    The trick is to match parenthesis before the ,

    ~          Pattern delimiter
    '
    [^']       All charaters but not a single quote
    ++         one or more time in [possessive][1] mode
    '
    |          or
    \([^)]++\) the same with parenthesis
    |          or
    [^,]       All characters but not a comma
    ++
    ~
    

    if you have more than one delimiter like quotes (that are the same for open and close), you can write your pattern like this, using a capture group:

    $string="a,b,c,(d,e,f),g,'h, i j.',k,°l,m°,#o,p#,@q,r@,s";
    
    preg_match_all('~([\'#@°]).*?\1|\([^)]++\)|[^,]++~', $string,$result);
    print_r($result[0]);
    

    explanation:

    (['#@°])   one character in the class is captured in group 1
    .*?        any character zero or more time in lazy mode 
    \1         group 1 content
    

    With nested parenthesis:

    $string="a,b,(c,(d,(e),f),t),g,'h, i j.',k,°l,m°,#o,p#,@q,r@,s";
    
    preg_match_all('~([\'#@°]).*?\1|(\((?>[^()]++|(?-1)?)*\))|[^,]++~', $string,$result);
    print_r($result[0]);
    
    0 讨论(0)
提交回复
热议问题