如何删除Perl字符串中的空格?

时间:2010-01-08 05:14:17

标签: perl

如果我使用值$myString声明变量'3 '(请注意空格)。 是否有任何函数可以删除返回值的空白区域。 有点像SomeFun($myString)然后返回'3'(没有空格)。

#!C:\Perl\bin\perl.exe
use strict;
use warnings;
use Data::Dumper; 

my $fh = \*DATA; 

print Dumper parse_constant_spec( $fh ); 


# Parse a constant spec file. 
# Pass in a handle to process. 
# As long as it acts like a file handle, it will work. 
sub parse_constant_spec { 
    my $fh = shift; 

    my %spec; 

    # Until file is done: 
        # Read in a whole block 
    while( my $block = read_block($fh) ) { 

        # Parse the and return key/value pairs for a hash. 
        my %constant = parse_block( $block ); 

        # Store a ref to the hash in a big hash of all blocks, keyed by constant_name. 
        $spec{ $constant{const_name} } = \%constant; 

    } 

    # Return ref to big hash with all block data 
    return \%spec; 
} 

# Read a constant definition block from a file handle. 
# void return when there is no data left in the file. 
# Otherwise return an array ref containing lines to in the block.  
sub read_block { 
    my $fh = shift; 

    my @lines; 
    my $block_started = 0; 

    while( my $line = <$fh> ) { 

        $block_started++ if $line =~ /^constant/; 

        if( $block_started ) { 

            last if $line =~ /^\s*$/; 

            push @lines, $line; 
        } 
    } 

    return \@lines if @lines; 

    return; 
} 


sub parse_block { 
    my $block = shift; 
    my ($start_line, @attribs) = @$block; 

    my %constant; 

    # Break down first line: 
    # First separate assignment from option list. 
    my ($start_head, $start_tail) = split /=/, $start_line; 

    # work on option list 
    my @options = split /\s+/, $start_head; 

    # Recover constant_name from options: 
    $constant{const_name} = pop @options; 
    $constant{options} = \@options; 

    # Now we parse the value/type specifier 
    @constant{'type', 'value' } = parse_type_value_specifier( $start_tail ); 

    # Parse attribute lines. 
    # since we've already got multiple per line, get them all at once. 
    chomp @attribs; 
    my $attribs = join ' ', @attribs; 

    #  we have one long line of mixed key = "value" or key = <TYPE VALUE>  

    @attribs = $attribs =~ /\s*(\w+\s+=\s+\w+\s+|\w+\s+=\s+".*?"|\w+\s+=\s+<.*?>)\s*/g; 

    for my $attrib ( @attribs ) { 
        warn "$attrib\n"; 
        my ($name, $value) = split /\s*=\s*/, $attrib; 

        if( $value =~ /^"/ ) {  
            $value =~ s/^"|"\s*$//g; 
        } 
        elsif( $value =~ /^</ ) { 
           $value = [ parse_type_value_specifier( $start_tail ) ]; 
        } 
        else { 
            warn "Bad line"; 
        } 

        $constant{ $name } = $value; 
    } 

    return %constant; 
} 

sub parse_type_value_specifier { 
    my $tvs = shift; 

    my ($type, $value) = $tvs =~ /<(\w+)\s+(.*?)>/; 

    return $type, $value; 
} 

__DATA__ 
constant fixup GemEstabCommDelay = <U2 20> 
    vid = 6 
    name = "ESTABLISHCOMMUNICATIONSTIMEOUT" 
    units = "s" 
    min = <U2 0> 
    max = <U2 1800> 
    default = <U2 20> 


constant fixup private GemConstantFileName = <A "C:\\TMP\\CONST.LOG"> 
    vid = 4 
    name = ""  units = "" 


constant fixup private GemAlarmFileName = <A "C:\\TMP\\ALARM.LOG"> 
    vid = 0 
    name = "" 
    units = ""   

输出:

D:\learning\perl>hello1.pl
vid = 6
Bad line at D:\learning\perl\hello1.pl line 102, <DATA> line 8.
name = "ESTABLISHCOMMUNICATIONSTIMEOUT"
units = "s"
min = <U2 0>
max = <U2 1800>
default = <U2 20>
vid = 4
Bad line at D:\learning\perl\hello1.pl line 102, <DATA> line 13.
name = ""
units = ""
vid = 0
Bad line at D:\learning\perl\hello1.pl line 102, <DATA> line 18.
name = ""
units = ""
$VAR1 = {
          'GemAlarmFileName' => {
                                  'vid' => '0      ',
                                  'options' => [
                                                 'constant',
                                                 'fixup',
                                                 'private'
                                               ],
                                  'value' => '"C:\\\\TMP\\\\ALARM.LOG"',
                                  'name' => '',
                                  'type' => 'A',
                                  'const_name' => 'GemAlarmFileName',
                                  'units' => ''
                                },
          'GemEstabCommDelay' => {
                                   'vid' => '6      ',
                                   'options' => [
                                                  'constant',
                                                  'fixup'
                                                ],
                                   'value' => '20',
                                   'min' => [
                                              'U2',
                                              '20'
                                            ],
                                   'name' => 'ESTABLISHCOMMUNICATIONSTIMEOUT',
                                   'max' => [
                                              'U2',
                                              '20'
                                            ],
                                   'default' => [
                                                  'U2',
                                                  '20'
                                                ],
                                   'type' => 'U2',
                                   'units' => 's',
                                   'const_name' => 'GemEstabCommDelay'
                                 },
          'GemConstantFileName' => {
                                     'vid' => '4      ',
                                     'options' => [
                                                    'constant',
                                                    'fixup',
                                                    'private'
                                                  ],
                                     'value' => '"C:\\\\TMP\\\\CONST.LOG"',
                                     'name' => '',
                                     'type' => 'A',
                                     'const_name' => 'GemConstantFileName',
                                     'units' => ''
                                   }
        };

D:\learning\perl>

您可能会注意到'vid' => '0 ',(注意空白区域)

the answer上面的代码。我正在研究它。 : - )

谢谢。

12 个答案:

答案 0 :(得分:24)

$myString =~ s/^\s*(.*?)\s*$/$1/;

这将修剪双方的空白。

从右边开始:

$myString =~ s/\s*$//;

答案 1 :(得分:13)

如果您的空白区域只是空格,则以下代码将删除所有空格:

$mystring =~ tr/ //ds;

答案 2 :(得分:11)

sub trim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}


print trim($myString)

答案 3 :(得分:8)

试试这个:

# Delete leading/trailing whitespace.
$string =~ s/^\s+|\s+$//g;

答案 4 :(得分:5)

另一个可能的替代解决方案是来自CPAN的Text::Trim,它将“从字符串中删除前导和/或尾随空格”。它具有trim功能,可以满足您的需求。

答案 5 :(得分:2)

sub trim
{
    my $str = $_[0];
    $str=~s/^\s+|\s+$//g;
    return $str;
}

print trim(" 4 ");

答案 6 :(得分:2)

这是一个子程序,它允许你从字符串中删除前导和尾随空格,同时从字符串中删除多余的空格,并用单个空格替换它。

-- routine

sub unspace { my @stringer = @_ ? @_ : $; $ = join( ' ', split(' ')) for @stringer; return wantarray ? @stringer : "@stringer"; }

-- usage

$MySpacedString = ' String with tabs double-spaces and other whitespace areas. '; $MyCleanString = unspace($MySpacedString);

答案 7 :(得分:1)

只是查看你的程序,我找到了3个可以改进或修复的地方。

如果我的代码格式不正确,我道歉。 : - (

在你的函数parse_block(...)中,有3个项需要注意。

@attribs = $attribs =~ /\s*(\w+\s+=\s+\w+\s+|\w+\s+=\s+".*?"|\w+\s+=\s+<.*?>)\s*/g;

在vid =&gt;之后消除空白区域'6',只是不要在第一个子正则表达式的末尾包含\ s +。

将其写为:

@attribs = $attribs =~ /\s*(\w+\s+=\s+\w+|\w+\s+=\s+".*?"|\w+\s+=\s+<.*?>)\s*/g;  

$value = [ parse_type_value_specifier( $start_tail ) ];  

你想要这个:

$value = [ parse_type_value_specifier( $value ) ]; 

(注意函数的参数应该是$ value而不是$ start_tail。)你可能没有注意到这一点。

在@attributes的循环中,if / else条件中的'else'在'value'具有普通值('value'中没有“”或&lt; ...&gt;项目)时会生效。

更新:将

parse_type_value_specifier(...)
中的参数更改为$ value。它(错误地)表示为$ attrib。

答案 8 :(得分:0)

从Transact SQL中删除变量$test (eq rtrim(ltrim(@sStr))中的空格:

$test =~s/^\s*(\S*)\s*$/$1/;

答案 9 :(得分:0)

如果您愿意使用CPAN模块,那么String::Util或更经济的Text::Trim将是可能的选择。

修剪琴弦是每个人都喜欢建造的自行车棚之一!有关TIMTOWDI乐趣的一小部分样本,请参阅perlmaven tutorial的简短@szabgab

答案 10 :(得分:0)

我建议您使用Text::Trim模块,该模块提供ltrimrtrimtrim,所有这些模块都会修改传递的参数,或{ {1}}如果您不提供参数。它不是核心模块,因此可能需要安装

答案 11 :(得分:0)

删除字符串中的所有空格:

$string =~ s/ //g;