PHP regex not new line
10,707
Solution 1
The dot .
does not match newlines unless you use the s
modifier.
>>> preg_match("/./", "\n")
0
>>> preg_match("/./s", "\n")
1
Solution 2
As written on the PHP Documentation page on Preg Modifiers, a dot .
does NOT include newlines, only when you use the s
modifier.
Source
Solution 3
The following regular expression should match any character except newlines
/[^\n]+/
Solution 4
The default behavior shouldn't match a new line. Because the "s" modifier is used to make the dot match all characters, including new lines. Maybe you can provide an example to look at?
Solution 5
#!/usr/bin/env php
<?php
$test = "Some\ntest\nstring";
// Echos just "Some"
preg_match('/(.*)/', $test, $m);
echo "First test: ".$m[0]."\n";
// Echos the whole string.
preg_match('/(.*)/s', $test, $m);
echo "Second test: ".$m[0]."\n";
So I don't know what is wrong with your program, but it's not the regex (unless you have the /s
modifier in your actual application.
Author by
David
Updated on June 04, 2022Comments
-
David almost 2 years
How do I have a regex statement that accepts any character except new lines. This includes anything but also includes new lines which is not what i want:
"/(.*)/"