Regular Expressions Cookbook, 2nd Edition
by Steven Levithan
Published by
O'Reilly Media, Inc., 2012
and
Tags
| 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, XRegExp, PCRE, Perl, Python, Ruby |
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] Item1=Value1 Item2=[Value2] ; [SectionA] ; The SectionA header has been commented out ItemA=ValueA ; ItemA is not commented out, and is part of Section1 [Section2] Item3=Value3 Item4 = Value4
Given the string just shown, this regex finds two matches. The
first match extends from the beginning of the string up to and including
the empty line before [Section2]. The
second match extends from the start of the Section2 header until the end
of the string.
Recipe 9.13 shows how to match INI section headers. Recipe 9.15 does the same for INI name-value pairs.
Techniques used in the regular expressions in this recipe are discussed in Chapter 2. Recipe 2.1 explains which special characters need to be escaped. Recipe 2.2 explains how to match nonprinting characters. Recipe 2.3 explains character classes. Recipe 2.5 explains anchors. Recipe 2.9 explains grouping. Recipe 2.12 explains repetition.