2011-02-02 64 views
1

如何使用Perl的正則表達式搜索找到文件結尾:Perl的正則表達式匹配<foo> EOF

-------\r\n<eof> 

16進製表示是:

2D 2D 2D 2D 0D 0A (the end of the file) 

我在UltraEdit,其中說:它使用Boost Perl正則表達式語法。

我已經想通了足夠使用:

----\x0d\x0a 

這確實發現我想要的線路,但只有其中包括數百人是不是在文件的結尾:

whatever 
------------  <- matches this also, which I don't want! 
whatever 
------------  <- matches this also, which I don't want! 
whatever 

回答

2

UltraEdit的正則表達式引擎以基於行的方式工作。除此之外,這意味着它不會區分行尾和文件結束。

它不知道\z\Z結束字符串標記。此外,像-----\r\n(?!.)這樣的負向前瞻斷言在UE中不起作用。

所以UE的正則表達式引擎讓你在這裏。你可以做的是使用宏:

InsertMode 
ColumnModeOff 
HexOff 
Key Ctrl+END 
Key UP ARROW 
PerlReOn 
Find RegExp "-----\r\n" 
IfFound 
# Now do whatever you wanted to do... 
EndIf 

並讓UE將它應用於所有文件。

0

你是否需要迭代文件中的每一行並使用正則表達式?如果沒有,只是seek你需要檢查字符串是否相等文件中現貨:

open my $fh, '<', $the_file; 
seek $fh, 2, -6;   # seek to the end of file minus 6 bytes 
read $fh, my $x, 6;   # read 6 bytes into $x 
if ($x eq "----\r\n") { 
    print "The end of file matches ----\\x0d\\x0a\n"; 
} else { 
    print "The end of file doesn't match ----\\x0d\\x0a\n"; 
} 
0

這裏是去這個用UltraEdit JavaScript的一種方式。

使用UltraEdit.activeDocument.bottom()轉到文件的底部; 使用UltraEdit.activeDocument.currentPos();存儲您的當前位置。

向後搜索「\ r \ n」 再次使用UltraEdit.activeDocument.currentPos();並將結果與​​之前的位置進行比較以確定這是否實際上是文件末尾的cr/lf。

根據這些角色位置進行任何替換/插入操作,或者放出一個消息框來宣佈結果。