【发布时间】:2020-04-01 10:14:09
【问题描述】:
我有一个字符串,并根据冒号(:) 或分号(;) 进行拆分,后跟空格。
这里是代码 sn-p。
my $string = "MAJOR RCB_Board: Circuit Disconnected";
my ($rest, $text) = split(/;|:\s+/, $string);
print "Rest=$rest ** Text=$text\n";
但在这里我也想用字符串打印分割分隔符。在此示例中 (:)。
所以我应该得到如下输出:
Rest=MAJOR RCB_Board: ** Text=Circuit Disconnected
【问题讨论】:
-
您可以将正则表达式放在括号中以返回分隔符,请参阅the documentation
-
另请注意
\s+仅出现在右侧替代项中,您可能指的是/[;:]\s+/。