2017-08-08 159 views
-1

之前得到字符串中的最後一個數字你知道一種合併這兩個正則表達式的方法嗎?
或任何其他方式來獲取最後的\之前的最後6位數字。Perl正則表達式在

,我想最終的結果是從字符串100144

\\XXX\Extract_ReduceSize\MonitoringExport\dev\files\100144\

這裏有一些事情我已經試過

(.{1})$ 

擺脫尾隨的\的產生的字符串

\\XXX\Extract_ReduceSize\MonitoringExport\dev\files\100144

.*\\ 

擺脫一切的最後\之前導致100144

我使用的軟件,只需要一條線。所以我可以進行2個電話。

+1

*「軟件我使用」 *:什麼軟件? –

+0

'm |。* /(。*)/ $ |'。但是,如果這些是文件路徑,還有其他方法 – zdim

回答

1

既然你想要最後一個,([^\\]*)\\$將是適當的?這與最後一個斜槓之前的儘可能多的非斜線字符匹配。或者,如果您不想提取第一組,則可以使用([^\\]+)(?=\\$)進行前瞻。

1

此代碼顯示了兩種不同的解決方案。希望它能幫助:

use strict; 
use warnings; 

my $example = '\\XXX\Extract_ReduceSize\MonitoringExport\dev\files\100144\\'; 

# Method 1: split the string by the \ character. This gives us an array, 
# and then, select the last element of that array [-1] 
my $number = (split /\\/, $example)[-1]; 
print $number, "\n"; # <-- prints: 100144 

# Method 2: use a regexpr. Search in reverse mode ($), 
# and catch the number part (\d+) in $1 
if($example =~ m!(\d+)\\$!) { 
    print $1, "\n"; # <-- prints: 100144 
} 
1

本工程以提取數字的最後一段:

(\d+)(?=\\$)