One regular expression to match parts in various positions

Posted by richsage on Stack Overflow See other posts from Stack Overflow or by richsage
Published on 2010-04-23T13:29:32Z Indexed on 2010/04/23 13:33 UTC
Read the original article Hit count: 228

Filed under:
|
|

Hi all,

I'm trying to parse a DSN (from a Symfony application) using regular expressions, in order to link with a secondary application, but using the same database.

The DSN I currently have is:

mysql:dbname=my_db_name;host=localhost

with a regex of:

/^(\w+):(dbname=(\w+))?;?(host=(\w+))?/

(using preg_match()). This matches OK, but fails in my test environment because the DSN elements are switched around, thus:

mysql:host=localhost;dbname=my_testdb_name

I could just switch them round, yes :-) but I'm sure that extraction of the host and dbname parts from both DSNs is possible with a single regular expression, and I'd like to be able to enhance my knowledge at the same time ;-) Is there a way I can do this?

© Stack Overflow or respective owner

Related posts about regex

Related posts about dsn