开发者

How can I extract parts of this filename in Perl?

In my Excel sheet with column Changeset开发者_开发知识库 I am getting the changeset like:

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)

  • the part before @@ (e.g-here C:\ccviews\hgdasdff-9302\dfcsz\ahgrt\kjhssl\ASGHLS)
  • the last character of the string (e.g-here 3)


This sounds too complicated for a regular split, you need an ordinary regex like this:

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


From Regular Expression Mastery by Mark Dominus:

Randal's Rule

  • Randal Schwartz (author of Learning Perl [and also a Stack Overflow user]) says:

Use capturing or m//g when you know what you want to keep.

Use split when you know what you want to throw away.

How can I extract parts of this filename in Perl?

You know what you want to keep, so use m//g as in Leon Timmermans's answer.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜