The .
character in a php regex accepts all characters, except a newline. What can I use to accept ALL characters, including newlines?
views:
60answers:
4
+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
2010-10-26 17:27:25
Aren't there supposed to be forward slashes at the beginning and end of a regexp?
TheAdamGaskins
2010-10-26 17:29:14
Can be, but any pair of delimiters will do.
Tim Pietzcker
2010-10-26 17:31:01
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
2010-10-26 17:31:37
+1 Depending on your needs `m` is an option as well. But based on the OP, `s` is the way to go.
Jason McCreary
2010-10-26 17:33:48
+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
2010-10-26 17:29:05
@gnomed: The `.` in a character class does not mean "any character". It means "a dot". Character classes have their own syntax. ;-)
Tomalak
2010-10-26 17:38:51
@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
2010-10-26 17:43:13
@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
2010-10-26 17:50:40
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
2010-10-26 17:55:18