4

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:

"/(.*)/"
1
  • You're wrong. See jeremy Ruten's answer. Commented Nov 14, 2009 at 23:11

6 Answers 6

16

The dot . does not match newlines unless you use the s modifier.

>>> preg_match("/./", "\n")
0
>>> preg_match("/./s", "\n")
1
Sign up to request clarification or add additional context in comments.

Comments

4

As written on the PHP Documentation page on Preg Modifiers, a dot . does NOT include newlines, only when you use the s modifier. Source

Comments

1

The following regular expression should match any character except newlines

/[^\n]+/

1 Comment

yes, except the original matches the empty string and this one requires at least one non-linebreak character... "/[^\n]*/" (and if you're on Windows, you might want to exclude \r as well...)
1

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?

Comments

1
#!/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.

Comments

0

this is strange, because by default the dot (.) does not accept newlines. Most probably you have a "\r" (carriage return) character there, so you need to eliminate both: /[^\r\n]/

ah, you were using /s

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.