perl分裂奇怪的行为

 mobiledu2502871243 发布于 2023-02-08 16:17

我提前道歉,这可能是一个非常愚蠢的问题,有一个显而易见的解决方案,它逃避了一个相当初学者的眼睛,或者它也可能已经在Stackoverflow中作为一个解决的问题,但我缺乏关于究竟是什么的知识寻找是阻止我实际找到答案.

我有一个字符串:

$s = FOO: < single blankspace> BAR  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  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 个回答
  • 问题是*允许零空间(零或更多)的量词,你必须使用+,这意味着1或更多.

    请注意,F和O之间的空间正好为零.

    2023-02-08 16:20 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有