Using RegEx to identify parts of a database connection string

I am trying to deal with regular expressions:

I have a database connection string, and I would like to use a regular expression to identify specific keys and values ​​inside it.

for example

server=foo;database=bar;uid=foo;pwd=bar 

I need something to return "database = bar;" using a database key to identify it, ideally this would be case insensitive. I can do this using regular code, but I think this is exactly the thing for which regular expressions have been developed.

+6
regex
source share
1 answer
 database=([^;]*); 

gotta do the trick. It matches the string database= , followed by any sequence of zero or more semicolons, followed by a semicolon. The sequence of non-seasonal points is enclosed in brackets, so you can subsequently extract the text corresponding to this part of the regular expression.

How to specify case insensitivity and extracting the value in brackets depends on the language you use.

+8
source share

All Articles