简体   繁体   English

如何在Perl中提取此文件名的部分内容?

[英]How can I extract parts of this filename in Perl?

In my Excel sheet with column Changeset I am getting the changeset like: 在包含Changeset列的Excel工作表中,我得到的变更集如下:

C:\ccviews\hgdasdff-9302\dfcsz\ahgrt\kjhssl\ASGHLS@@\main\ajsdkljlat\hahdasdhfk\1\test.txt\sub\hsdaklfl\3

I need to use split function in a Perl script so that there will be two ouput (input as the above string) 我需要在Perl脚本中使用split函数,这样就会有两个输出(输入为上面的字符串)

  • the part before @@ (eg-here C:\\ccviews\\hgdasdff-9302\\dfcsz\\ahgrt\\kjhssl\\ASGHLS ) @@之前的部分(例如 - 这里C:\\ccviews\\hgdasdff-9302\\dfcsz\\ahgrt\\kjhssl\\ASGHLS
  • the last character of the string (eg-here 3) 字符串的最后一个字符(例如 - 这里3)

对于常规拆分来说这听起来太复杂了,你需要像这样的普通正则表达式:

my ($first, $second) = / ^ (.+?) @@ .* (.) $ /x;

From Regular Expression Mastery by Mark Dominus: 来自Mark Dominus的正则表达精通

Randal's Rule 兰德尔的规则

Use capturing or m//g when you know what you want to keep. 当你知道要保留什么时,使用捕获或m//g

Use split when you know what you want to throw away. 当你知道要扔掉什么时使用split

兰德尔施瓦茨

You know what you want to keep, so use m//g as in Leon Timmermans's answer . 你知道你想要保留什么,所以在Leon Timmermans的回答中使用m//g

I think this is what you want, or do you need it all in one statement? 我认为这是你想要的,还是你需要一个声明?

 my ($before, $after) = split '@@', $input;
 my $last_char = substr($after, -1, 1);

Adding to the two answers already, you can try the following using only split function: 已添加两个答案,您可以仅使用拆分功能尝试以下操作:

$s = 'C:\ccviews\hgdasdff-9302\dfcsz\ahgrt\kjhssl\ASGHLS@@\main\ajsdkljlat\hahdasdhfk\1\test.txt\sub\hsdaklfl\3';

@temp = split/@@/,$s;
$part1 = $temp[0]; # C:\ccviews\hgdasdff-9302\dfcsz\ahgrt\kjhssl\ASGHLS

@temp = split//,$s;
$part2 = $temp[-1]; # 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM