Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex perl分裂奇怪的行为_Regex_Perl_Split - Fatal编程技术网

Regex perl分裂奇怪的行为

Regex perl分裂奇怪的行为,regex,perl,split,Regex,Perl,Split,我提前表示歉意,这可能是一个非常愚蠢的问题,有一个明显的解决方案,在perl初学者看来,这可能是一个逃过眼睛的问题,或者它也可能是在Stackoverflow中作为一个已解决的问题,但我对要查找的内容缺乏了解,这使我无法真正找到答案 我有一个字符串,比如: $s = FOO: < single blankspace> BAR <some whitespace character> some more text with whitespace that can sp

我提前表示歉意,这可能是一个非常愚蠢的问题,有一个明显的解决方案,在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.
我希望,$instType取FOO:,没有任何周围的空格,在实际的测试字符串中有一个冒号,据我所知,它将保留在$instType中。然后,很明显,$inst会以类似的方式获取值栏,没有任何周围的空格,最后,还可以使用$trail来获取字符串的其余部分

然而,我得到: $instType接受F,这只是一个字符, $inst接受O,字符串中第二个位置的单个字符 $trail需要O:BAR和其他

我如何解决这个问题


PS perl是5.18.0

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

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

您写道:

#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
您也可以而且应该在子程序之外使用
my
。将其与
使用strict
结合使用可防止出现如下愚蠢错误:

$some_field = 'bar';
if ( $some_feild ) { ... }

如果将这些语句分开,那么要找到那个bug可能会非常困难。

呃,非常感谢。它要求我等4分钟,直到accepting@Sean:弗雷什·威纳希登!噢,丹克·塞尔,真的!您将
\s*
设置为可选,因此只要有可能,它就不匹配任何内容。
$some_field = 'bar';
if ( $some_feild ) { ... }