#!/usr/bin/perl use warnings; use strict; my $line = '|Match_regex_1a_P_a|Match_regex_2_a|Match_regex_1b_P_b|Match_regex_2_b|'; my $regex1 = qr/\|(?[^|]+)_P_/; my $regex2 = qr/\|(?(?![^|]+_P_)[^|]+)/; use Test::More; my @matches1 = map "Match_regex_1$_", qw( a b ); while ($line =~ /$regex1/g) { is $+{DataCapture}, shift @matches1; } my @matches2 = map "Match_regex_2_$_", qw( a b ); while ($line =~ /$regex2/g) { is $+{DataCapture}, shift @matches2; } done_testing();