How do I remove white space in a Perl string?

后端 未结 12 1936
离开以前
离开以前 2020-12-31 09:34

If I declared a variable $myString with the value \'3 \' (notice the white space). Is there any function to remove the white space for the return v

12条回答
  •  半阙折子戏
    2020-12-31 10:09

    Just looking over your program, I found 3 spots that could be improved or fixed.

    I apologize if my code doesn't format well. :-(

    In your function parse_block(...), there are 3 items that need attention.

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

    To eliminate the white space after vid => '6 ', just don't include the \s+ at the end of your first sub-regex.

    Write it as:

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

    You want this instead:

    $value = [ parse_type_value_specifier( $value ) ]; 
    

    (Note that the parameter to the function should be $value and not $start_tail.) You probably didn't notice this.

    In the loop for @attributes, the 'else' in the if/else condition excutes when the 'value' has a plain value, (no "" or <...> items in 'value').

    Update: Changed parameter in

    parse_type_value_specifier(...)
    to $value. It was (incorrectly) stated as $attrib.

提交回复
热议问题