use strict; use warnings; while (<DATA>) { while (m
As single line:
perl -nlwe 'while (m#/(\*?)(.*?)\1/#g) { print $2 }' input.txt
The inner while loop will cycle through all matches using the /g modifier. The backreference \1 function ensures that we will only match the same open / close tags.
If you need to match blocks that span multiple lines, you need to reset the input:
use strict; use warnings; $/ = undef; while (<DATA>) { while (m
Single line:
perl -0777 -nlwe 'while (m#/(\*?)(.*?)\1/#sg) { print $2 }' input.txt
The switch -0777 and $/ = undef will cause the file to break, which means that all files are read into the scalar. I also added the /s modifier to a wildcard . match newline characters.
Explanation for a regular expression: m#/(\*?)(.*?)\1/#sg
m#
The "magic" here is that a star * is required for backreference only if it is found before it.
TLP
source share