How to Replace Trailing Spaces With Perl Regular Expressions
A regular expression is a series of one or more characters that you use to match patterns in a computer program. In Perl, you often make use of regular expressions. You can write a function that trims the trailing spaces in a string and use the function anywhere in you Perl program. Use a regular expression in Perl's string pattern matching function, specifying that you want the regular expression to only match at the end of the string.
Instructions
-
-
1
Open a new Perl program. Insert the cursor at the beginning of the file.
-
2
Type the following code:
sub trim($) {
my $string = shift;
$string =~ s/\s+$//;
return $string;
}
This function creates a temporary file that takes the value of the string passed to the function. It then uses a regular expression to remove the trailing spaces. The "\s" tells the function to only look for white space characters and the "$" says to only look at the end of the string. After remove the trailing spaces, the function returns the variable to the main program.
-
-
3
Save the program file.
-
1