Alex
Alex

Reputation: 180

How to get the first match of a pattern?

How can I get just the c999752_ABC_PAT from the string below?

$file = q(M:\c999752_ABC_PAT\Informatica_AVOB\ABC_infa\dummy\TestDeliver.txt);

Upvotes: 1

Views: 73

Answers (3)

ysth
ysth

Reputation: 98388

use File::Spec::Win32;

my $file = q(M:\c999752_ABC_PAT\Informatica_AVOB\ABC_infa\dummy\TestDeliver.txt);
my ($volume, $directories) = File::Spec::Win32->splitpath($file);
my @directories = File::Spec::Win32->splitdir($directories);

print $directories[1], "\n";

Upvotes: 0

Kenosis
Kenosis

Reputation: 6204

Or use what Eammon suggested:

my $firstPart = ( split /\\/, $file )[1];

Upvotes: 1

Anirudha
Anirudha

Reputation: 32787

You can use this regex

^.*?\\(.*?)\\.*$
|   |   |     |->matches till the end
|   |   |->your content matched till the first occurance of \
|   |->match lazily till the first \
|->start of the text

Group1 captures your data

Upvotes: 1

Related Questions