Php – Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset

PHPpreg-matchregex

i'm trying to change the preg_match check from url checking to username checking which is min/max 2-16chrs, dash, space & hypen acceptable. i'm getting this error

Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 14

if(empty($string) || preg_match("#^([\w- ]{2,16}*(?:.[\w- ]{2,16}*)+):?(d+)?/?#i", $string))

old code that looked for URL

if(empty($string) || preg_match("#^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?#i", $string))

Best Answer

The problem is here:

[\w- ]{2,16}*

You can't use {2,16} and * together, you can only use one or the other.

If you were looking to match groups of 2 to 16 [\w- ]s, at least 0 times, wrap it in a subpattern and attach the * outside:

(?:[\w- ]{2,16})*