2017-08-08 156 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+)(?=\\$)