gpt4 book ai didi

regex - Perl拆分奇怪的行为

转载 作者:行者123 更新时间:2023-12-04 13:32:26 24 4
gpt4 key购买 nike

我事先表示歉意,这可能是一个非常愚蠢的问题,有一个显而易见的解决方案正在逃脱Perl中初学者的视线,或者它也可能已经在Stackoverflow中作为已解决的问题出现,但是我缺乏关于到底要做什么的知识。寻找阻碍了我真正找到答案。

我有一个像这样的字符串:

$s = FOO: < single blankspace> BAR <some whitespace character> some more text with     whitespace that can span over multiple lines, i.e. has \n in them ; 

#please excuse the lack of quotes, and large text describing the character in angular brackets, but in this example, but I have the string correctly defined, and in plase of <blankspace> I have the actual ASCII 32 character etc.

现在,我想以这种方式拆分$ s:
($instType, $inst, $trailing) = split(/\s*/, $s, 3);
#please note that i do not use the my keyword as it is not in a subroutine
#but i tested with my, it does not change the behavior

我希望$ instType的值为FOO:,而没有任何周围的空格,在实际的测试字符串中会有一个冒号,并且据我所知,它将保留在$ instType中。然后很显然,可以预期$ inst的值与BAR相似,而没有任何周围的空格,然后最后一个人也可以依靠$ trail来获取字符串的其余部分。

但是,我得到:
$ instType采用F,即单个字符,
$ inst将O(字符串中第二个字符)
$ trail需要O:BAR和其余的。

我该如何解决这个问题?

PS perl是5.18.0

最佳答案

问题是量词*允许零空间(零或更多),您必须改用+,这意味着1或更多。

请注意,F和O之间的空间恰好为零。

关于regex - Perl拆分奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20733048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com