Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Trimming (removing) leading and trailing whitespaces from a string is a common task in text processing. Here's a tutorial on how to perform this operation in Perl.
1. Basic trimming using regular expressions:
The following regular expression can be used to trim both leading and trailing whitespace from a string:
my $str = " Hello, world! "; $str =~ s/^\s+|\s+$//g; print "'$str'"; # Outputs 'Hello, world!'
^\s+
matches one or more (+
) whitespace characters (\s
) at the beginning (^
) of the string.|\s+$
matches one or more (+
) whitespace characters (\s
) at the end ($
) of the string.2. Separate trimming for leading and trailing spaces:
If you want to separate the trimming for some reason (for example, to only trim spaces at the beginning or end of the string), you can do so:
Trimming leading spaces:
$str =~ s/^\s+//;
Trimming trailing spaces:
$str =~ s/\s+$//;
3. Creating a trim function:
For convenience, you can create a trim
function to use throughout your code:
sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s; } my $str = " Hello, world! "; $str = trim($str); print "'$str'"; # Outputs 'Hello, world!'
4. Trimming using external modules:
There are many CPAN modules available that offer string trimming capabilities, among other string utility functions. One such module is String::Util
.
To use String::Util
:
First, you might need to install it:
cpan install String::Util
Then, you can use its trim
function:
use String::Util 'trim'; my $str = " Hello, world! "; $str = trim($str); print "'$str'"; # Outputs 'Hello, world!'
Trimming strings is an essential part of many text processing tasks. The above methods provide a variety of ways to perform this operation in Perl, from direct regex approaches to the use of helper modules from CPAN. Choose the method that best fits your needs and coding style.
Removing leading spaces in Perl:
my $text = " Leading spaces"; $text =~ s/^\s+//; # Remove leading spaces print "$text\n"; # Output: "Leading spaces"
Trimming trailing spaces in Perl:
my $text = "Trailing spaces "; $text =~ s/\s+$//; # Remove trailing spaces print "$text\n"; # Output: "Trailing spaces"
Using regular expressions for trimming in Perl:
my $text = " Trim spaces "; $text =~ s/^\s+|\s+$//g; # Remove leading and trailing spaces print "$text\n"; # Output: "Trim spaces"
Perl trim function example:
sub trim { my $string = shift; $string =~ s/^\s+|\s+$//g; return $string; } my $text = " Trim spaces "; $text = trim($text); print "$text\n"; # Output: "Trim spaces"
Removing whitespace characters from a string in Perl:
my $text = " Remove \t whitespace \n characters "; $text =~ s/\s+//g; # Remove all whitespace characters print "$text\n"; # Output: "Removewhitespacecharacters"
Strip leading and trailing spaces in Perl:
my $text = " Strip spaces "; $text =~ s/^\s+|\s+$//g; # Remove leading and trailing spaces print "$text\n"; # Output: "Strip spaces"
Trimming newline characters in Perl:
my $text = "\nTrim newlines\n"; $text =~ s/^\n+|\n+$//g; # Remove leading and trailing newlines print "$text\n"; # Output: "Trim newlines"
Perl strip whitespace from a variable:
my $text = " Strip whitespace "; $text =~ s/\s+//g; # Remove all whitespace characters print "$text\n"; # Output: "Stripwhitespace"