【发布时间】:2018-10-23 01:12:28
【问题描述】:
我发现了如何用空格分割字符串,但这只考虑了一个字符。就我而言,我已将 cmets 粘贴到包含换行符和空格的文件中。我用这个字符串分隔它们:[|]
所以我需要将我的 $string 拆分为一个数组,例如 $string =
This is a comment.
This is a newline.
This is the end[|]This is second comment.
This is second newline.
[|]Last comment
被拆分为 $array[0]、$array[1] 和 $array[2],其中包括换行符和空格。用 [|] 分隔
我在网上找到的每个示例都使用单个字符(例如空格或换行符)来拆分字符串。在我的情况下,我必须使用更具体的标识符,这就是为什么我选择了 [|] 但在拆分它时遇到了麻烦。
我试图将其限制为通过单个“|”进行解析带有此代码的字符:
my @words = split /|/, $string;
foreach my $thisline (@words) {
print "This line = '" . $thisline . "'\n";
但这似乎将整个字符串逐个字符拆分为@words。
【问题讨论】: