php - preg_split() 具有多个模式,不拆分引用的字符串
php - preg_split() with multiple patterns not splitting quoted string
我需要将一个段落拆分成句子。
这就是我对正则表达式有点困惑的地方。
我已经提到这个 question,这个 Q 被标记为重复。但这里的问题是不同的。
这是我需要拆分的字符串的示例:
hello! how are you? how is life
live life, live free. "isnt it?"
这是我试过的代码:
$sentence_array = preg_split('/([.!?\r\n|\r|\n])+(?![^"]*")/', $paragraph, -1);
我需要的是:
array (
[0] => "hello"
[1] => "how are you"
[2] => "how is life"
[3] => "live life, live free"
[4] => ""isnt it?""
)
我得到的是:
array(
[0] => "hello! how are you? how is life live life, live free. "isnt it?""
)
当我在字符串中没有任何引号时,拆分会按要求进行。
感谢任何帮助。谢谢。
我认为你的基于某些标点符号拆分的问题已经解决了,除了在双引号的情况下它失败了。我们可以将解决方案表述为当看到这样的标点符号时我们应该拆分,当看到这个标点符号后跟双引号时 或 。
当前一个字符与您的标记之一匹配时应该发生拆分并且后面不是双引号,或者前两个字符应该是一个标记and 双引号。这意味着拆分以下模式,该模式使用环视:
(?<=[.!?\r\n])(?=[^"])|(?<=[.!?\r\n]")(?=.)
代码示例:
$input = "hello! how \"are\" \"you?\" how is life\nlive life, live free. \"isnt it?\"";
$sentence_array = preg_split('/(?<=[.!?\r\n])(?=[^"])|(?<=[.!?\r\n]\")(?=.)/', $input, -1);
print_r($sentence_array);
Array ( [0] => hello! [1] => how "are" "you?" [2] => how is life
[3] => live life, live free. [4] => "isnt it?" )
您的正则表达式存在一些问题,主要是将组构造与字符 classes 混淆。字符 class 中的竖线 |
字面意思是 |
。没有什么特别的意思。
你需要的是:
("[^"]*")|[!?.]+\s*|\R+
这首先尝试匹配用双引号括起来的字符串(并捕获内容)。然后尝试匹配 [!?.]
中设置为拆分的任何标点符号。然后找到任何类型的换行符。
PHP:
var_dump(preg_split('~("[^"]*")|[!?.]+\s*|\R+~', <<<STR
hello! how are you? how is life
live life, live free. "isnt it?"
STR
, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
输出:
array(5) {
[0]=>
string(5) "hello"
[1]=>
string(11) "how are you"
[2]=>
string(11) "how is life"
[3]=>
string(20) "live life, live free"
[4]=>
string(10) ""isnt it?""
}
我需要将一个段落拆分成句子。 这就是我对正则表达式有点困惑的地方。
我已经提到这个 question,这个 Q 被标记为重复。但这里的问题是不同的。
这是我需要拆分的字符串的示例:
hello! how are you? how is life
live life, live free. "isnt it?"
这是我试过的代码:
$sentence_array = preg_split('/([.!?\r\n|\r|\n])+(?![^"]*")/', $paragraph, -1);
我需要的是:
array (
[0] => "hello"
[1] => "how are you"
[2] => "how is life"
[3] => "live life, live free"
[4] => ""isnt it?""
)
我得到的是:
array(
[0] => "hello! how are you? how is life live life, live free. "isnt it?""
)
当我在字符串中没有任何引号时,拆分会按要求进行。
感谢任何帮助。谢谢。
我认为你的基于某些标点符号拆分的问题已经解决了,除了在双引号的情况下它失败了。我们可以将解决方案表述为当看到这样的标点符号时我们应该拆分,当看到这个标点符号后跟双引号时 或 。
当前一个字符与您的标记之一匹配时应该发生拆分并且后面不是双引号,或者前两个字符应该是一个标记and 双引号。这意味着拆分以下模式,该模式使用环视:
(?<=[.!?\r\n])(?=[^"])|(?<=[.!?\r\n]")(?=.)
代码示例:
$input = "hello! how \"are\" \"you?\" how is life\nlive life, live free. \"isnt it?\"";
$sentence_array = preg_split('/(?<=[.!?\r\n])(?=[^"])|(?<=[.!?\r\n]\")(?=.)/', $input, -1);
print_r($sentence_array);
Array ( [0] => hello! [1] => how "are" "you?" [2] => how is life
[3] => live life, live free. [4] => "isnt it?" )
您的正则表达式存在一些问题,主要是将组构造与字符 classes 混淆。字符 class 中的竖线 |
字面意思是 |
。没有什么特别的意思。
你需要的是:
("[^"]*")|[!?.]+\s*|\R+
这首先尝试匹配用双引号括起来的字符串(并捕获内容)。然后尝试匹配 [!?.]
中设置为拆分的任何标点符号。然后找到任何类型的换行符。
PHP:
var_dump(preg_split('~("[^"]*")|[!?.]+\s*|\R+~', <<<STR
hello! how are you? how is life
live life, live free. "isnt it?"
STR
, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
输出:
array(5) {
[0]=>
string(5) "hello"
[1]=>
string(11) "how are you"
[2]=>
string(11) "how is life"
[3]=>
string(20) "live life, live free"
[4]=>
string(10) ""isnt it?""
}