正则表达式匹配三个引用字符串

时间:2014-03-26 03:51:37

标签: php regex pcre

我有这个字符串(比如python doc):

"""
1 string with two lines
in this area
"""
"""
2 string, but with more "quotes"
"""
"""
3 string
multiline and "multiquote"
"""

我需要:

array(
[0] => 1 string with two lines
    in this area

[1] => 2 string, but with more "quotes"
[2] => 3 string
    multiline and "multiquote"

)

但是,我有:

/(?<!""")(?<=(?!""").)"(?!""")/im 

/(\x22\x22\x22)[\w\s\d\D\W\S.]+(?\x22\x22\x22)/i

2 个答案:

答案 0 :(得分:2)

请参阅此https://eval.in/126887

preg_match_all("/\"{3}(.*?)\"{3}/s", $str, $matches);

答案 1 :(得分:2)

为什么你不像这样编码

$re = '/"""(.*?)"""/is'; 
$str = '"""
1 string with two lines
in this area
"""
"""
2 string, but with more "quotes"
"""
"""
3 string
multiline and "multiquote"
"""'; 

preg_match_all($re, $str, $matches);
echo "<pre>";
print_r($matches[1]);

output:

Array
(
    [0] => 
1 string with two lines
in this area

    [1] => 
2 string, but with more "quotes"

    [2] => 
3 string
multiline and "multiquote"

)