RegEx: remove numbers at the end of a line and text after a specific line
I need to delete some parts of the file:
The content might look like this:
Lorem ipsum 12, 14
Lorem 29
34
Lorem s. anything
It should become as follows:
Lorem ipsum
Lorem
Lorem
1) All numbers must be removed (there may be multiple numbers separated by commas). If there is nothing else, number, line should be removed
2) If there is an "s.", Everything after that point on that line should be removed.
This is my attempt:
$myfile = fopen("file.txt", "r") or die("Unable to open file!");
while(!feof($myfile)) {
// remove some parts and output
$newString = preg_replace("/\d+$/","",fgets($myfile));
echo newString . "<br>";
}
fclose($myfile);
+3
source to share
2 answers
Find this regex:
( *\d+(, *\d+)*|s\..*)$
And the answer is an empty line.
Demo version of RegEx
You can also use this regex (thanks @hwnd):
(?:[\d, ]+| *s\..*)$
+2
source to share
\d+(\s*,\s*\d+)*|\bs\..*$
Try it. Replace empty string
. View a demo.
http://regex101.com/r/kP8uF5/10
$re = "/\\d+(\\s*,\\s*\\d+)*|\\bs\\..*$/im";
$str = "Lorem ipsum 12, 14\nLorem 29\n34\nLorem s. anything\nvcbvcbv";
$subst = "";
$result = preg_replace($re, $subst, $str);
+1
source to share