我想从字符串中删除日期标识符和*。
$string = "*102015 Supplied air hood";
$output = "Supplied air hood";
我用过
$string =~ s/[#\%&\""*+]//g;
$string =~ s/^\s+//;
我应该用什么来获得字符串值=“提供的风帽”;
提前致谢
答案 0 :(得分:3)
要删除从字符串到第一个空格的所有内容,您可以编写
$str =~ s/^\S*\s+//;
答案 1 :(得分:1)
您的模式不包含数字。它将删除*
,但不会删除任何其他内容。如果您要删除*
后跟六位数字并在字符串开头删除空白,请执行以下操作:
$string =~ s/^\*\d{6} //;
但是,如果该字符串始终包含这样的模式,则不需要正则表达式替换。你可以简单地取一个子串。
my $output = substr $string, 8;
这将从第9个字符开始分配$string
的内容
答案 2 :(得分:0)
下面的脚本可以满足您的需求,假设日期始终显示在行的开头,并且后面只有一个空格。
use strict;
use warnings;
while (<DATA>)
{
# skip one or more characters not a space
# then skip exactly one space
# then capture all remaining characters
# and assign them to $s
my ($s) = $_ =~ /[^ ]+ (.*)/;
print $s, "\n";
}
__DATA__
*110115 first date
*110115 second date
*110315 third date
输出是:
first date
second date
third date