Perl 正则表达式匹配eof
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UltraEdit 的正则表达式引擎以基于行的方式工作。这意味着它不区分行尾和文件尾。
它也不知道
\z
或\Z
字符串结尾标记。此外,像-----\r\n(?!.)
这样的否定前瞻断言在 UE 中不起作用。UE 的正则表达式引擎可以让您失望。您可以做的是使用宏:
并让 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:
and have UE apply that to all your files.
您是否需要迭代文件中的每一行并使用正则表达式?如果没有,只需
seek
到文件中您需要的位置并检查字符串是否相等: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:这是使用 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.