user2245731
user2245731

Reputation: 549

Perl: remove a part of string after pattern

I have strings like this:

trn_425374_1_94_-
trn_12_1_200_+
trn_2003_2_198_+

And I want to split all after the first number, like this:

trn_425374
trn_12
trn_2003

I tried the following code:

$string =~ s/(?<=trn_\d)\d+//gi;

But returns the same as the input. I have been following examples of similar questions but I don't know what I'm doing wrong. Any suggestion?

Upvotes: 1

Views: 16890

Answers (4)

RobEarl
RobEarl

Reputation: 7912

Use \K to preserve the part of the string you want to keep:

$string =~ s/trn_\d+\K.*//;

To quote the link above:

\K

This appeared in perl 5.10.0. Anything matched left of \K is not included in $& , and will not be replaced if the pattern is used in a substitution.

Upvotes: 0

Borodin
Borodin

Reputation: 126722

If you are running Perl 5 version 10 or later then you have access to the \K ("keep") regular expression escape. Everything before the \K is excluded from the substitution, so this removes everything after the first sequence of digits (except newlines)

s/\d+\K.+//;

with earlier versions of Perl, you will have to capture the part of the string you want to keep, and replace it in the substitution

s/(\D*\d+).+/$1/;

Note that neither of these will remove any trailing newline characters. If you want to strip those as well, then either chomp the string first, or add the /s modifier to the substitution, like this

s/\d+\K.+//s;

or

s/(\D*\d+).+/$1/s;

Upvotes: 3

Miguel Prz
Miguel Prz

Reputation: 13792

your regexr should be:

$string =~ s/(trn_\d+).*/$1/g;

It substitutes the whole match by the memorized at $1 (which is the string part you want to preserve)

Upvotes: 0

Birei
Birei

Reputation: 36252

Do grouping to save first numbers of digits found and use .* to delete from there until end of line:

#!/usr/bin/env perl

use warnings;
use strict;

while ( <DATA> ) { 
    s/(\d+).*$/$1/ && print;
}

__DATA__
trn_425374_1_94_-
trn_12_1_200_+
trn_2003_2_198_+

It yields:

trn_425374
trn_12
trn_2003

Upvotes: 2

Related Questions