Regular Expressions 101

Community Patterns

INI Parser

4

Regular Expression
PCRE (PHP <7.3)

/
(^( +|)\[(?<name>[^#;\r\n]+)\])|(^( +|)(?<key>[^\[\]\r\n=#;]+)( |)=( |)(?<value>[^#;\\\r\n]*(?:\\.[^#;\\\r\n]*)*))|((#|;)(?<comm>.+)$)
/
gm

Description

Created for purpose of reading an INI file in C# applications. Supports comments and comment escape characters. I plan to add more escape characters (such as =).

s    - Section
scp  - Section Comment Prefix
sc   - Section Comment
k    - Key
v    - Value
kvcp - KeyValue Comment Prefix
kvc  - KeyValue Comment
cp   - Comment Prefix
c    - Comment

I am open to any suggestion that may improve upon this regex, and/or would allow me to learn more. <br />

Created by Raxdiam
Submitted by Raxdiam - 4 years ago