2011-02-02 69 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。

根据这些角色位置进行任何替换/插入操作,或者放出一个消息框来宣布结果。