Regex to remove first line in a text file

Discuss and share scripts and script files...
Post Reply
hermhart
Posts: 213
Joined: 13 Jan 2015 18:41

Regex to remove first line in a text file

Post by hermhart »

Hello, I've been searching for a while and tried many things and having a difficult time coming up with the right regex to remove the first line of a text file.

What I have is:

Code: Select all

 $oldContent = readfile("$testFile");

 $newContent = regexreplace($oldContent, "^([0-9a-zA-Z_.#\-]*[a-zA-Z0-9]{3}[0-9]{3}~[0-9a-z\-]*\.[a-z]*)(.*)$", $1);

 $newContent = regexreplace("$newContent", "\s|\S*", "");  // I cannot get this to completely delete the first line for the text to be written

 writefile("$testFile", $newContent);
Does anyone have any suggestions?

highend
Posts: 13317
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Regex to remove first line in a text file

Post by highend »

There is no need for any regex and it would be trivial if the engine would be 100% PCRE compatible (which isn't the case)

Code: Select all

$newContent = gettoken($newContent, 2, <crlf>, , 2);
One of my scripts helped you out? Please donate via Paypal

hermhart
Posts: 213
Joined: 13 Jan 2015 18:41

Re: Regex to remove first line in a text file

Post by hermhart »

Thank you very much, highend!

With a little tweaking to some other lines, that worked out great!

Post Reply