我一直在阅读、搜索和试用不同的方法来编写正则表达式,例如 p{L}、[az] 和 \w,但我似乎无法得到我想要的结果。
我有一个由带有标点符号的完整句子组成的数组,我正在使用以下 pre_match 解析一个数组,它在保留单词和标点符号方面效果很好。
preg_match_all('/(\w+|[.;?!,:])/', $match, $matches)
Run Code Online (Sandbox Code Playgroud)
但是,我现在有这样的词:
我希望能够保持这些单词的完整性(连接),但我当前的 preg_match 将它们分解为单个单词。
preg_match_all('/(p{L}-p{L}+|[.;?!,:])/', $match, $matches)
Run Code Online (Sandbox Code Playgroud)
和;
preg_match_all('/((?i)^[\p{L}0-9_-]+|[.;?!,:])/', $match, $matches)
Run Code Online (Sandbox Code Playgroud)
我从这里找到的
但无法达到这个预期的结果:
Array ( [0] A, [1] word, [2] like_this, [3] connected, [4] ; ,[5] with-relevant-punctuation)
Run Code Online (Sandbox Code Playgroud)
理想情况下,我还可以考虑特殊字符,因为其中一些单词可能带有重音
我对我的代码中的一个问题感到困惑,并希望其他人可以帮助我解释为什么我的循环省略(array[0])了数组的第一个元素.
foreach ($a as $key => $val) {
for ($i=0; $i<count($val); $i++) {
$x = $i; //this helps me jump logical arguments without the use of else
// First Test
if (isset($val[$i+2]) && $x = $i) {
//Do a bunch of stuff
if (isset(the stuff done above)) {
// do other things and reset $i to jump through the array
$i=$i+2;
}
else {
unset($things);
unset($otherthings);
}
}
}
// Second Test
if (isset($val[$i+1]) && $x = …Run Code Online (Sandbox Code Playgroud)