use strict;
my $str = '***This is strong and em.***
The quick brown fox
jumps over the lazy dog
Lorem Ipsum
I should match
- I should NOT match
Le sigh
> Why am I matching?
1. Nonononono!
* Aaaagh!
# Stahhhp!
Hello, World!
==
---
***
***
***
***
***
* * *
* * *
* * *
* * *
___
___
___
___
';
my $regex = qr/(?:(?<=^\n)|(?<=\A)) # Needs 1 or more new lines or start of string
(?<para_all>
(?<para_prefix>[ ]{0,3}) # Possibly some leading spaces, but less than a tab worth
(?<para_content>
(?:
(?! # some line content not starting with those exceptions
[[:blank:]\h]{0,3}
(?:
\*{1}[ \t]+ # a list
|
(?:\*(?:[ ]?\*){2,}) # a horizontal line
|
(?:\-*(?:[ ]?\-){2,}) # a horizontal line
|
(?:\_*(?:[ ]?\_){2,}) # a horizontal line
|
[>+-=\#]
|
\d+\. # ordered list
|
\`{3,} # code block
|
\~{3,} # code block extended
)
)
)
.+
(?!\n(?:[=-]+))
(?:\n|$)
)+
)/mxp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html