1, the Named grouping format is (? <grp name>), the reverse reference is \K<GRP name>
2. The result of a named grouping match exists in the variable%+ variable, taking the named grouping value, $+{grp name.
3, the reverse reference can also be used \g1,\g{1},\g{-1},\g{-2}, negative representation of the penultimate grouping of groups.
Cases:
Method 1:
Local $_ = ' Buster and Buster ';
if (/(\s+) (and|or) \1/i)
{
Print "I found the same name twice:$1\n";
print "\$2=$2\n";
}
Else
{
Print "Not matched\n";
}
#-----------------------------------------------------------
Method 2:
Local $_ = ' Buster and Buster ';
if (/(? <key>\s+) (And|or) \k<key>/i)
{
Print "I found the same name twice:$+{key}\n";
print "\$2=$2\n";
}
Else
{
Print "Not matched\n";
}
#-----------------------------------------------------------
Method 3:
Local $_ = ' Buster and Buster ';
if (/(? <key>\s+) (?: and|or) \g{-1}/i)
{
Print "I found the same name twice:$+{key}\n";
}
Else
{
Print "Not matched\n";
}