Perl 正则表达式匹配eof

发布于 2024-10-15 06:28:06 字数 497 浏览 1 评论 0原文

如何使用 Perl 正则表达式搜索来查找以以下内容结尾的文件:

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

在十六进制中,这是:

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

How can I use the Perl regex search to find files ending in:

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

In hex this is:

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

I'm in UltraEdit, which says it uses Boost Perl regex syntax.

I've figured enough out to use:

----\x0d\x0a

which does find the lines I want, but only amongst hundreds of others that aren't at the end of the file:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空‖城人不在 2024-10-22 06:28:06

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 将其应用到您的所有文件。

UltraEdit's regex-engine works in a line-based way. This means among other things that it does not discriminate between end of line and end-of-file.

It doesn't know the \z or \Z end-of-string markers, either. Also, a negative lookahead assertion like -----\r\n(?!.) doesn't work in UE.

So UE's regex engine lets you down here. What you could do is to use a macro:

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

and have UE apply that to all your files.

戏舞 2024-10-22 06:28:06

您是否需要迭代文件中的每一行并使用正则表达式?如果没有,只需 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";
}

Do you need to iterate through every line in the file and use a regex? If not, just seek to the spot in the file you need and check for string equality:

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";
}
童话 2024-10-22 06:28:06

这是使用 UltraEdit JavaScript 实现此目的的一种方法。

使用 UltraEdit.activeDocument.bottom() 转到文件底部;
使用 UltraEdit.activeDocument.currentPos();存储您当前的位置。

向后搜索“\r\n”
再次使用 UltraEdit.activeDocument.currentPos();并将结果与​​前一个位置进行比较,以确定这实际上是否是文件末尾的 cr/lf。

根据这些字符位置进行您想要的任何替换/插入,或者弹出一个消息框来宣布结果。

Here is one way to go about this using UltraEdit JavaScript.

Go to the bottom of the file with UltraEdit.activeDocument.bottom();
Use UltraEdit.activeDocument.currentPos(); to store your current position.

Search backwards for"\r\n"
Again, use UltraEdit.activeDocument.currentPos(); and compare the results to the previous position to determine if this is, in fact, a cr/lf at the end of the file.

Do whatever replacement/insertion you had in mind based on these character positions, or throw up a message box announcing the results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文