>我需要检查最后一行下面是否有东西。(如果可以将光标放在其下方,则它应该匹配。
This is the first line
This is the last line
|
注意:This is the last line
下面的|
表示最后一行下方有一行空行,因此您可以将光标放在最后一行下方。
我怎样才能检测到这一点?我用preg_match
试过了,但是使用linux命令的解决方案也可以。
https://regex101.com/r/mY9wQ3/2
这是一个带有正则表达式的解决方案:
它会检查文本缓冲区末尾的 2 个连续 EOL,在所有可能的变体中。
法典
<?php
// The function ----------------------------------------------------------
function hasEOLatEnd($text) {
return (preg_match('/.*?('r'n|'n'r|'r|'n){2,2}$/s', $text));
}
// -----------------------------------------------------------------------
// Tests -----------------------------------------------------------------
$text_1 = "This a one line - without EOL";
$text_2 = "This is one line - with EOL
";
$text_3 = "These are two lines -
without EOL";
$text_4 = "These are two lines -
with EOL
";
echo '$text_1 has ' . (hasEOLatEnd($text_1) ? 'EOL' : 'NO EOL') . ' at the end<br />';
echo '$text_2 has ' . (hasEOLatEnd($text_2) ? 'EOL' : 'NO EOL') . ' at the end<br />';
echo '$text_3 has ' . (hasEOLatEnd($text_3) ? 'EOL' : 'NO EOL') . ' at the end<br />';
echo '$text_4 has ' . (hasEOLatEnd($text_4) ? 'EOL' : 'NO EOL') . ' at the end<br />';
// ----------------------------------------------------------------------
?>
结果
$text_1 has NO EOL at the end $text_2 has EOL at the end $text_3 has NO EOL at the end $text_4 has EOL at the end
Do tail -n1 <filename> | grep '^$'
-- 如果文件的最后一行为空,这将返回 0。
function no_space_after($str){
return (trim($str) == $str);
}