我一直想弄清楚我的印刷声明有什么问题。到目前为止,我有这段代码:
if ($message =~ /Status changed from (New|In Progress|Feedback) to Completed/) {
my ($author, $assignee, $target, $issue, $title) = (
"",
"",
"",
"",
"",
);
if ($message =~ /\* Author: (\b.*)/) {
$author = $1;
}
if ($message =~ /\* Assigned To: (\b.*)/) {
$assignee = $1;
}
if ($message =~ /\* Target version: (\b.*)/) {
$target = $1;
}
if ($message =~ /Issue #(\d+) has been updated by (\b.*)/) {
$issue = $1;
}
if ($message =~ /(Task|Story) #(\d+): (\b.*)/) {
$title = $3;
}
print "Author: $author\n";
print "Assignee: $assignee\n";
print "Issue: $issue\n";
print "Title: $title\n";
print "Target: $target\n";
my $result = sprintf("%s completed issue %s %s due on %s\n", $assignee, $issue, $title, $target);
print "$result\n";
print "Author: $author\n";
print "Assignee: $assignee\n";
print "Issue: $issue\n";
print "Title: $title\n";
print "Target: $target\n";
出于某种奇怪的原因,我的输出结果如下:
Author: Miguel Morales
Assignee: Miguel Morales
Issue: 1257
Title: Get wireless access to servers
Target: sprint-apr-2
due on sprint-apr-27 Get wireless access to servers
Author: Miguel Morales
Assignee: Miguel Morales
Issue: 1257
Title: Get wireless access to servers
Target: sprint-apr-2
你可以看到当我一次打印一个变量时,输出打印得很好,但是当我把变量混合起来时,它会搞得一团糟。有什么想法吗?注意我是perl的新手,但我过去做过一些C.我不确定这是与$ 1,$ 2,$ 3变量或我使用sprintf的方式有关,但我尝试过print,printf并且仍然是相同的。
以下是$message
示例:
Issue #1257 has been updated by Miguel Morales.
Status changed from In Progress to Completed
% Done changed from 40 to 90
----------------------------------------
Task #1257: Get wireless access to servers
http://test.sample.com/issues/1257#change-4651
* Author: Miguel Morales
* Status: Completed
* Priority: Normal
* Assigned To: Miguel Morales
* Category:
* Target version: sprint-apr-2
----------------------------------------
--
You have received this notification because you have either subscribed to it, or are involved in it.
答案 0 :(得分:2)
您的问题不是print
,$message
包含CRLF行结尾。在正则表达式中,.
匹配任何非LF的字符(这意味着它与CR匹配)。当您打印CR时,光标会返回到左边距,导致后续文本覆盖您已经打印的内容。当您单独打印变量时,您看不到这一点,因为在CR之后打印的下一个字符是\n
(LF),它将光标移动到下一行的开头。
由于您没有向我们展示您如何获得$message
,因此很难说删除CR的最佳方法是什么。也许您应该使用:crlf
图层打开文件。
删除CR的一种方法是
$message =~ s/\r//g;
在开始提取所需的位之前。或者,您可以将(\b.*)
替换为不会提取CR的内容。例如,您可以使用(.*\S)
,这要求匹配以非空白字符结束。