Page 1 of 1

Regex to remove first line in a text file

Posted: 10 Mar 2019 04:43
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?

Re: Regex to remove first line in a text file

Posted: 10 Mar 2019 05:56
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);

Re: Regex to remove first line in a text file

Posted: 10 Mar 2019 17:53
by hermhart
Thank you very much, highend!

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