Perl regex to match <foo>eof

1.1k views Asked by At

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
3

There are 3 answers

0
Tim Pietzcker On

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.

0
mob On

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";
}
0
Robert Rapplean On

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.