Thursday, December 08, 2005

Regular expression to parse .ini files




Regular expression to fetch <key-value> pairs from the old-fashioned .ini files:

((\s)*(?<Key>([^\=^\s^\n]+))[\s^\n]* # key part (surrounding whitespace stripped)
\=
(\s)*(?<Value>([^\n^\s]+(\n){0,1}))) # value part (surrounding whitespace stripped)

Would fetch patterns in form of:



webProxyPort=8080
   webProxyPort  =   8080 
webProxyPort =
8080

Key and Value appears in form of named captures with whitespaces stripped on both ends.