Я пытаюсь получить все группы из трех слов из строки, которая может состоять из нескольких предложений, не выходя за границы предложений. У меня это работает для слов, которые имеют только стандартные буквы алфавита:
preg_match_all("/(?=(\b(\w+)(?:\s+(\w+)\b|$)(?:\s+(\w+)\b|$)))/",$utext,$matches);
print_r($matches[1]);
Но он падает там, где есть апострофы или дефисы. Итак, с этим образцом текста:
The quick brown fox's feet jumped over the lazy dog. The rain falls head-first in the plain.
Я хочу этот список:
Я пытался использовать [\w'-] для каждого \w выше, но это дает некоторые странности:
Array ( [0] => The quick brown [1] => quick brown fox's [2] => brown fox's feet [3] => fox's feet jumped [4] => 's feet jumped [5] => s feet jumped [6] => feet jumped over [7] => jumped over the [8] => over the lazy [9] => the lazy dog [10] => The rain falls [11] => rain falls head-first [12] => falls head-first in [13] => head-first in the [14] => -first in the [15] => first in the [16] => in the plain )
Что мне не хватает? Спасибо.






Просто замените \w на [^\s.] (не пробел и не точку) и удалите слово boudaries. Другое изменение заключается в добавлении чередования «начало строки ИЛИ пробел» в начале регулярного выражения:
$text = "The quick brown fox's feet jumped over the lazy dog. The rain falls head-first in the plain.";
preg_match_all("/(?=((?<=^|\s)[^\s.]+(?:\s+[^\s.]+|$)(?:\s+[^\s.]+|$)))/",$text,$matches);
print_r($matches[1]);
Выход:
Array
(
[0] => The quick brown
[1] => quick brown fox's
[2] => brown fox's feet
[3] => fox's feet jumped
[4] => feet jumped over
[5] => jumped over the
[6] => over the lazy
[7] => the lazy dog
[8] => The rain falls
[9] => rain falls head-first
[10] => falls head-first in
[11] => head-first in the
[12] => in the plain
)
Объяснение регулярного выражения:
(?= # lookahead
( # start group 1
(?<=^|\s) # lookbehind, make sure we have beginning of line or space before
[^\s.]+ # 1 or more non space, non dot
(?: # non capture group
\s+ # 1 or more spaces
[^\s.]+ # 1 or more non space, non dot
| # OR
$ # end of line
) # end group
(?: # non capture group
\s+ # 1 or more spaces
[^\s.]+ # 1 or more non space, non dot
| # OR
$ # end of line
) # end group
) # end group 1
) # end lookahead
Изменить в соответствии с комментарием.
$text = "The quick brown fox's feet jumped over the lazy dog. The rain falls head-first in the plain. 'This is a quote,' I say, and that's that.";
preg_match_all("/(?=((?<=^|\s|')(?:(?<=[a-zA-Z])'(?=[a-zA-Z])|[^\s.,'])+(?:\s+(?:(?<=[a-zA-Z])'(?=[a-zA-Z])|[^\s.,'])+|$){2}))/",$text,$matches);
print_r($matches[1]);
Выход:
Array
(
[0] => The quick brown
[1] => quick brown fox's
[2] => brown fox's feet
[3] => fox's feet jumped
[4] => s feet jumped
[5] => feet jumped over
[6] => jumped over the
[7] => over the lazy
[8] => the lazy dog
[9] => The rain falls
[10] => rain falls head-first
[11] => falls head-first in
[12] => head-first in the
[13] => in the plain
[14] => This is a
[15] => is a quote
[16] => and that's that
)
Объяснение регулярного выражения:
(?= # lookahead
( # start group 1
(?<=^|\s|') # lookbehind, make sure we have beginning of line or space or quote before
(?: # start non capture group
(?<=[a-zA-Z]) # lookbehind, make sure we have a letter before
' # a single quote
(?=[a-zA-Z]) # lookahead, make sure we have a letter after
| # OR
[^\s.,'] # not a space or dot or comma or single quote
)+ # group may appear 1 or more times
(?: # non capture group
\s+ # 1 or more spaces
(?: # non capture group
(?<=[a-zA-Z]) # lookbehind, make sure we have a letter before
' # a single quote
(?=[a-zA-Z]) # lookahead, make sure we have a letter after
| # OR
[^\s.,'] # not a space or dot or comma or single quote
)+ # group may appear 1 or more times
| # OR
$ # end of line
){2} # end group, must appear twice
) # end group 1
) # end lookahead
Во втором примере законными триграммами были бы: Это а, это цитата и все (потому что я говорю, что предложение слишком короткое, чтобы его можно было определить). Я предполагаю, что это, возможно, больше подходит для НЛП, чем для регулярного выражения.
@thehattery: Вы можете добавить запятую в класс символов, но для одинарной кавычки это немного сложнее, потому что вы хотите сохранить кавычку в fox's. Попробуйте заменить [^\s.]+ на (?:(?<=[a-zA-Z])'(?=[a-zA-Z]|[^\s.,'])+
отсутствует ) где-то?
Извините, (?:(?<=[a-zA-Z])'(?=[a-zA-Z])|[^\s.,'])+
Это гениально - спасибо! Он пропускает только This is a во втором примере, но отлично получает все остальное в обоих примерах.
@thehattery: Попробуйте: preg_match_all("/(?=((?<=^|\s|')(?:(?<=[a-zA-Z])'(?=[a-zA-Z])|[^\s.,'])+(?:\s+(?:(?<=[a-zA-Z])'(?=[a-zA-Z])|[^\s.,'])+|$){2}))/",$text,$matches);
Это гениально - спасибо. Теперь я вижу, что он борется с кавычками и запятыми, например, в
'This is a quote,' I say, and that's that.Но, возможно, я прошу здесь слишком многого!