tags:

views:

60

answers:

4

The . character in a php regex accepts all characters, except a newline. What can I use to accept ALL characters, including newlines?

+3  A: 

It's the the . character that means "every character" (edit: OP edited). And you need to add the option s to your regexp, for example :

preg_match("`(.+)`s", "\n");
Vincent Savard
Aren't there supposed to be forward slashes at the beginning and end of a regexp?
TheAdamGaskins
Can be, but any pair of delimiters will do.
Tim Pietzcker
Not in PHP. It has to start and end with a delimiter (you can choose it), and every character past the last delimiter is an option (i.e. U for ungreedy, i for case-insensitive, etc.)
Vincent Savard
+1 Depending on your needs `m` is an option as well. But based on the OP, `s` is the way to go.
Jason McCreary
+7  A: 

This is commonly used to capture all characters:

[\s\S]

You could use any other combination of "Type-X + Non-Type-X" in the same way:

[\d\D]
[\w\W]

but [\s\S] is recognized by convention as a shorthand for "really anything".

You can also use the . if you switch the regex into "dotall" (a.k.a. "single-line") mode via the "s" modifier. Sometimes that's not a viable solution (dynamic regex in a black box, for example, or if you don't want to modify the entire regex). In such cases the other alternatives do the same, no matter how the regex is configured.

Tomalak
Perfect! Thanks!
TheAdamGaskins
A: 

Have a look here:

http://www.php.net/manual/en/regexp.reference.escape.php

infinity
+1  A: 
gnomed
No, it would not.
SLaks
@gnomed: The `.` in a character class does not mean "any character". It means "a dot". Character classes have their own syntax. ;-)
Tomalak
@Tomalak: Thanks for the explanation, I just realized it now. I guess I should test my answers before I post them. I've edited my answer now.
gnomed
@gnomed: Common error. I see people do `[this|that|\d]` a lot, when they really mean `(this|that|\d)`. *P.S.: `(.|\n)` works but it may be slightly less efficient than a character class.*
Tomalak
Glad all I had was some metacharacter confusion. Dont think I would ever try to put an "|" inside "[]" I just like to avoid "()" whenever possible because they also are used to initialize special variables in Perl(and other languages) when something inside them matches.
gnomed