Regular Expressions 101

INI Parser for .NET

Open Workspace
  • .NET 10.0 (C#)
  • Match
1

Regular Expression

@"
(?=\S)(?<text>(?<comment>(?<open>[#;]+)(?:[^\S\r\n]*)(?<value>.*))|(?<section>(?<open>\[)(?:\s*)(?<value>[^\]]*\S+)(?:[^\S\r\n]*)(?<close>\]))|(?<entry>(?<key>[^=:\r\n\[\]]*\S)(?:[^\S\r\n]*)(?<delimiter>:|=)(?:\s*(?<value>\{(?:(?>(?:""(?:\\.|[^""])*""|//[^\r\n]*|/\*[\s\S]*?\*/|[^{}""/]+|/(?![/*])))|(?<o>\{)|(?<-o>\}))*(?(o)(?!))\})|((?:[^\S\r\n]*)(?<value>[^#;\r\n]*))))|(?<undefined>.+))(?<=\S)|(?<linebreaker>\r\n|\n)|(?<whitespace>(?>[^\S\r\n]+))
"
g

Description

INI Parser

A .NET regular expression for tokenizing INI-style configuration files.

Features

Supports:

  • Sections: [Section]
  • Key-value entries: key=value and key:value
  • Comments: # comment and ; comment
  • Whitespace and line breaks
  • Multiline object blocks using { ... }
  • Nested braces inside object blocks
  • C-style comments inside object blocks

Captured groups

GroupDescription
textAny meaningful text token
commentComment line
openOpening symbol (#, ;, [)
closeClosing symbol (])
sectionSection declaration
entryKey-value entry
keyEntry key
delimiterKey-value separator (= or :)
valueSection name, comment text, or entry value
undefinedUnknown text
linebreakerLine ending
whitespaceSpaces and tabs

Multiline values

Values wrapped in { } are treated as a single block:

Json =
{
    "name": "example",
    "items": [
        1,
        2
    ]
}

Inside blocks:

  • strings are ignored when counting braces;
  • // comments are ignored;
  • /* comments */ are ignored;
  • nested {} are supported.

Limitations

This regex performs structural parsing only.

It does not validate:

  • JSON syntax;
  • duplicate keys;
  • value types;
  • application-specific rules.

Use a dedicated parser after tokenization if full validation is required.

Compatibility

Designed for:

  • .NET System.Text.RegularExpressions
  • balancing groups support

Not compatible with regex engines without balancing group support.

Submitted by Pavel Bashkardin