O'Reilly logo

Regular Expressions Cookbook by Steven Levithan, Jan Goyvaerts

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.13. Match INI Section Blocks

Problem

You need to match each complete INI section block (i.e., a section header and all of the section’s parameter-value pairs), in order to split up an INI file or process each block separately.

Solution

Recipe 8.12 showed how to match an INI section header. To match an entire section, we’ll start with the same pattern from that recipe, but continue matching until we reach the end of the string or a [ character that occurs at the beginning of a line (since that indicates the start of a new section):

^\[[^\]\r\n]+](?:\r?\n(?:[^[\r\n].*)?)*
Regex options: ^ and $ match at line breaks (“dot matches line breaks” must not be set)
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Or in free-spacing mode:

^ \[ [^\]\r\n]+ ]  # Match a section header
(?:                # Followed by the rest of the section...
  \r?\n            #   Match a line break character sequence
  (?:              #   After each line starts, match...
    [^[\r\n]       #     Any character except "[" or a line break character
    .*             #     Match the rest of the line
  )?               #   The group is optional to allow matching empty lines
)*                 # Continue until the end of the section
Regex options: ^ and $ match at line breaks, free-spacing (“dot matches line breaks” must not be set)
Regex flavors: .NET, Java, PCRE, Perl, Python, Ruby

Discussion

This regular expression starts by matching an INI section header with the pattern ^\[[^\]\r\n]+], and continues matching one line at a time as long as the lines do not start with [. Consider the following subject text:

[Section1] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required