Random_Walk has asked for the wisdom of the Perl Monks concerning the following question:

Good Afternoon all

And sadly it looks like the afternoon of Perl Monks :(

But anyway, I'm looking for some regex guidance so I hope the gurus stil hang around here. I have a field in a | separated line of data that may contain _P_ or may not. The tool I am using to read it needs two distinct regex, one that will match |MyData_P_Rubbish| and only give me MyData and another regex that would match |YourData| and give me YourData but NOT pick up MyData from the previous example

I have \|(?<DataCapture>[^\|]+)_[P][_\|] That picks up the one with _P_ just fine, but I tried using negative zero width assertions and so to capture those without a _P_ something like \|(?<DataCapture>[^\|]+)(?!_[P]_) but it captures the _P_ too and I have no joy. Is it possible?

Is this possible?

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re: regex excluding some matches
by choroba (Cardinal) on Jan 04, 2022 at 20:21 UTC
    #!/usr/bin/perl use warnings; use strict; my $line = '|Match_regex_1a_P_a|Match_regex_2_a|Match_regex_1b_P_b|Mat +ch_regex_2_b|'; my $regex1 = qr/\|(?<DataCapture>[^|]+)_P_/; my $regex2 = qr/\|(?<DataCapture>(?![^|]+_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();

    Where

    [^|]+_P_

    matches exactly what you want to avoid.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: regex excluding some matches
by Cristoforo (Curate) on Jan 04, 2022 at 21:05 UTC
    To get the data without `_P_`, I think this will get it \|(?<DataCapture>(?:(?!_P_).)+)\|