http://www.perlmonks.org?node_id=644179


in reply to question : regex

I'm not sure if I've understood your requirement as I can't tell where the 'g' comes from in your output. However, this should get rid of any 'X_' occurances after a '__'.

use strict; use warnings; my @strings = qw{ KHI0339B__P_H_Vita_Korangi_Ind_A HGW6120A__S_Popalzai_Cross_A }; my $rxPatt = qr{(?<=__)[A-Z]_}; foreach my $string ( @strings ) { print qq{Original: $string\n}; $string =~ s{$rxPatt}{} while $string =~ $rxPatt; print qq{Modified: $string\n\n}; }

The output is

Original: KHI0339B__P_H_Vita_Korangi_Ind_A Modified: KHI0339B__Vita_Korangi_Ind_A Original: HGW6120A__S_Popalzai_Cross_A Modified: HGW6120A__Popalzai_Cross_A

I hope this is of use.

Cheers,

JohnGG