Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要创建一个正则表达式以仅匹配句子的第一个单词,当它等于或大于 4 个字符时。我在这里搜索真相和论坛,但无法做到..
例子:
"Christmas Baskets"> "Christmas" "Tea and infusions"> "Tea and infusions" "Beer"> "Beer"
我不确定我是否正确理解了这个问题,但这应该会给出您所描述的输出:
function get_first_word_or_sentence($sentence) { $word = strtok($sentence, ' '); return strlen($word) >= 4 ? $word : $sentence; }
这是 RegEx 的一种奇怪用法。以下将仅匹配句子开头长度超过 3 个字符的单词:
/^[^\s][^\s][^\s][^\s]+/