如何使用perl正则表达式删除起始空格?

时间:2011-08-08 23:16:10

标签: regex perl

在perl中指示空格的方法是什么? 如何使用perl正则表达式删除起始空格?

3 个答案:

答案 0 :(得分:6)

my $foo = " \t\n\r  hello, world!";
$foo =~ s/^\s+//;    # This is the line that removes the leading whitespace.
print "$foo\n";
print ord($foo) . "\n";

将显示:

hello, world!
104

104是h的ASCII字符代码,证明该字符串没有前导空格。

答案 1 :(得分:5)

这会在字符串(\s+)的开头搜索重复的空格(^),替换为空(即这里的分隔符之间的内容://):

$myString =~ s/^\s+//;

答案 2 :(得分:2)

$temp = "   Hello world";
#print $temp;
$temp =~ s/^\s+//;
print $temp;
相关问题