{"id":93,"date":"2009-09-02T06:35:07","date_gmt":"2009-09-02T06:35:07","guid":{"rendered":"http:\/\/www.adriangrigoras.com\/blog\/?p=93"},"modified":"2009-09-02T06:35:07","modified_gmt":"2009-09-02T06:35:07","slug":"trim-whitespaces-with-perl","status":"publish","type":"post","link":"https:\/\/adriangrigoras.com\/blog\/trim-whitespaces-with-perl\/","title":{"rendered":"Trim whitespaces with Perl"},"content":{"rendered":"<p>If you want to remove white spaces with Perl, you can use the following Reggular Expressions<\/p>\n<p>Trim all whitespaces: $value=~s\/s*\/\/g; # remove all the whitespace<\/p>\n<p>Or you can use function to trim the whitespaces from beggining, ending or both:<\/p>\n<blockquote>\n<address>\n<address># Perl trim function to remove whitespace from the start and end of the string<\/address>\n<address>sub trim($){<\/address>\n<address> my $string = shift;<\/address>\n<address> $string =~ s\/^s+\/\/;<\/address>\n<address> $string =~ s\/s+$\/\/;<\/address>\n<address> return $string;<\/address>\n<address>}<\/address>\n<address><\/address>\n<address># Left trim function to remove leading whitespace<\/address>\n<address>sub ltrim($){<\/address>\n<address> my $string = shift;<\/address>\n<address> $string =~ s\/^s+\/\/;<\/address>\n<address> return $string;<\/address>\n<address>}<\/address>\n<address><\/address>\n<address># Right trim function to remove trailing whitespace<\/address>\n<address>sub rtrim($){<\/address>\n<address> my $string = shift;<\/address>\n<address> $string =~ s\/s+$\/\/;<\/address>\n<address> return $string;<\/address>\n<address>}<\/address>\n<\/address>\n<\/blockquote>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"># Perl trim function to remove whitespace from the start and end of the string<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">sub trim($)<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">{<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"><span style=\"white-space: pre;\"> <\/span>my $string = shift;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"><span style=\"white-space: pre;\"> <\/span>$string =~ s\/^s+\/\/;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"><span style=\"white-space: pre;\"> <\/span>$string =~ s\/s+$\/\/;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"><span style=\"white-space: pre;\"> <\/span>return $string;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">}<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"># Left trim function to remove leading whitespace<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">sub ltrim($)<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">{<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"><span style=\"white-space: pre;\"> <\/span>my $string = shift;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"><span style=\"white-space: pre;\"> <\/span>$string =~ s\/^s+\/\/;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"><span style=\"white-space: pre;\"> <\/span>return $string;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">}<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"># Right trim function to remove trailing whitespace<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">sub rtrim($)<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">{<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"><span style=\"white-space: pre;\"> <\/span>my $string = shift;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"><span style=\"white-space: pre;\"> <\/span>$string =~ s\/s+$\/\/;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"><span style=\"white-space: pre;\"> <\/span>return $string;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">}<\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you want to remove white spaces with Perl, you can use the following Reggular Expressions Trim all whitespaces: $value=~s\/s*\/\/g; # remove all the whitespace Or you can use function to trim the whitespaces from beggining, ending or both: # Perl trim function to remove whitespace from the start and end of the string sub\u2026 <span class=\"read-more\"><a href=\"https:\/\/adriangrigoras.com\/blog\/trim-whitespaces-with-perl\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-perl"],"_links":{"self":[{"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":0,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"wp:attachment":[{"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}