$re = '/^(?!\s*$)\s*(?!\s)([^#\n\[=]+?)\s*?(?>#.*?$|(?>=\s*$|=\s*?(?:#?|(".*?"|[^\s].*?)\s*?)(?:\s*#.*?$)*?$)?$)/mi';
$str = '[mysqld]
skip-grant-tables # k
bind-address=127.0.0.1
port = 3307 # test
basedir=C:\\Program Files (x86)\\MySQL\\
datadir=C:\\Program Files (x86)\\MySQL\\data\\
default-storage-engine=INNODB
sql-mode= " STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,test NO_ENGINE_SUBSTITUTION" #test
max_connections=1000
tmp_table_size=1G
max_heap_table_size=1G
join_buffer_size=1M
query_cache_size =50M
query_cache_limit=50M
max_allowed_packet = 104857600
port =
= fh
thread_cache_size=8
#*** MyISAM Specific options
myisam_max_sort_file_size=100G
myisam_sort_buffer_size=35M
key_buffer_size=25M
read_buffer_size=64K
read_rnd_buffer_size=256K
sort_buffer_size=256K
#*** INNODB Specific options ***
innodb_flush_log_at_trx_commit=0
innodb_log_buffer_size=50M
innodb_buffer_pool_size=1000M
innodb_log_file_size=300M
innodb_thread_concurrency=8
innodb_flush_method=normal
innodb_autoinc_lock_mode=0
test=0
';
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