Regex to remove first line in a text file

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

Regex to remove first line in a text file

Post by hermhart » 10 Mar 2019 04:43

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: 8375
Joined: 06 Feb 2011 00:33

Re: Regex to remove first line in a text file

Post by highend » 10 Mar 2019 05:56

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 or highend (at) web (dot) de

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

Re: Regex to remove first line in a text file

Post by hermhart » 10 Mar 2019 17:53

Thank you very much, highend!

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

Post Reply