Question: I looking for a regex that returns true if string include characters or only 5 digits or include a string for php

tried this `^\D{0,5}$`

but it's not working 

for examples:

```
12345 - return true
1234455667&&&% . - true

123456 -false 
1234 - true 
FFGF - true 
empty string - true 

```

Answer: 
If I understood you correctly. You want `true` if any of the following is true:

* contains no more than 5 digits
* empty string
* contains at least non-digit

We can use `|` to separate these three conditions.

```
^(?:\d{0,5}|.*?\D.*)$

```

Note that the empty string case is also matched by `\d{0,5}`
PHP code ([test online](https://3v4l.org/ZQVXb)):

```
$str = '1234';
if (preg_match('~^(?(?=\d+$)\d{1,5}|.*)$~', $str)) {
    // true
}

```

If you are telling digits shouldn't be 5 or more you should build paths for them. One way is using conditional construct which PCRE has a support for `(?()...)`:

```
^(?(?=\d+$)\d{1,5}|.*)$

```

[Live demo](https://regex101.com/r/iholhx/2)

Breakdown:

* `^` Match beginning of input string
* `(?` Start of conditional
	+ `(?=\d+$)` Positive lookahead, if digits only
	+ `\d{1,5}` It should be at most five characters long
	+ `|` Otherwise
	+ `.*` Match any thing else
* `)` End of construct
* `$` Match end of input string