2012-11-04 41 views
1

如何從下面的字符串獲取c999752_ABC_PAT?如何獲得模式的第一個匹配項?

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

明顯的第一 - 你就不能按「\」爆炸? – Eamonn

+0

我用這個:=〜/(M\:\\.+)\\/;它給了我這個:M:\ ccadm01_QRF_DEV \ Informatica_QVOB \ QRF_infa,但我只想c999752_ABC_PAT或M:\ c999752_ABC_PAT – user1288329

回答

1

你可以使用這個表達式

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

Group1捕獲數據

+0

非常感謝! – user1288329

1

或者使用什麼Eammon建議:

my $firstPart = (split /\\/, $file)[1]; 
0
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"; 
相關問題