$re = '/^(
( # GENERAL before . (Dot)
(?!\w+\?) # DO NOT MATCH if contains ?
[\w\]\)]+ # Word, ] or ) characters 1 or more times
)|
(?:\((\w+)\))| # BRACKETS ()
([\w\]\)]+(?![?*+])) # BEFORE . (Dot) with ?, * or +
)/mx';
$str = 'abcdef.*g[0abc]{0,5}hi # abcdef
]1234vac.*12345 # ]1234vac
)1234vac.*12345 # )1234vac
(abc)+123 # abc
12345[abcde]+12345 # 12345
123?456 # 12
123\\?456 # 123?456
# --- No Prefix ---
[A-z]+12345
(1234vac.*12345
[a-z]+';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php