Question Details

No question body available.

Tags

regex perl multiline

Answers (3)

Accepted Answer Available
Accepted Answer
June 4, 2026 Score: 5 Rep: 68,426 Quality: High Completeness: 70%

it matches the paragraph it finds ^font.size = 12.0$ line in

If you want the whole paragraph, then it would be easiest to use paragraph mode. Either use switch -00, or set $/ = "". That way readline reads until double newlines \n\n+ and you can just print the whole line.

perl -00 -ne'print if /^\Qfont.size = 12.0\E$/mg;' input.txt
June 4, 2026 Score: 4 Rep: 393,442 Quality: Medium Completeness: 60%

print is short for print $, and $ contains the entire file. It's $& that contains the matched substring.

perl -gne'print $& while /^(?:\S.\n)font\.size = 12\.0\n(?:\S.\n)/mg'

The following also works:

perl -gne'print /^(?:\S.\n)font\.size = 12\.0\n(?:\S.\n)/mg'

Here the match is evaluated in list context. While a match is scalar context simply returns true or false depending on whether a match occurred, a match in list context returns all of the captured text, or all of the matched substrings if there are no captures. Since I have already eliminated the captures for being a useless drain, we can simply print the scalars returned by the match operator.


Note that this format normally accepts blank lines. You should really be searching for a (lack of) header.

perl -gne'print $& while / ^ \[\[ . \n (?: (?! \[\[ ) . \n ) font\.size \s = \s 12\.0 \n (?: (?! \[\[ ) . \n )* /xmg'
June 5, 2026 Score: 1 Rep: 133,861 Quality: Medium Completeness: 60%

If the one-liners work for you, stick with those. But, sometimes the assumptions those require aren't that good. I'm wary of the possibility that paragraph mode might break if there is a blank line under a heading, which means the regex might fail because the "lines" where already broken up on that whitespace.

I might be including to use a positive lookahead assertion in split to break on headers while keeping them as part of the item. The trick is to properly recognized a header, because TOML, for example, has multi-line strings where the [ at the start of a line might be inside a string.

However, once split, the rest of the problem is the same. It's just different chunks of text than what paragraph mode might give you.

use v5.10;

my $config =