我需要检查最后一行下面是否有东西。 (如果你可以把光标放在它下面,那么它应该匹配。)
This is the first line
This is the last line
|
注意:|
下面的This is the last line
表示最后一行下方有一个空行,因此您可以将光标放在最后一行下方。
我怎样才能发现这个?我尝试使用preg_match
,但是使用linux命令的解决方案也没问题。
答案 0 :(得分:1)
执行tail -n1 <filename> | grep '^$'
- 如果文件的最后一行为空,则返回0。
答案 1 :(得分:1)
这是一个带有正则表达式的解决方案:
它在所有可能的变体中检查文本缓冲区末尾的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
答案 2 :(得分:0)
semaphore.Release()