Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Regex Any Character

Tags:

regex

php

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

like image 914
Entity Avatar asked Oct 26 '10 17:10

Entity


1 Answers

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.

like image 52
Tomalak Avatar answered Sep 28 '22 08:09

Tomalak