Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a regular expression to remove a trailing slash in Perl?

Tags:

regex

perl

I would like to remove a trailing slash from a string. For example if i have a string called $test = "test/". How would I remove the slash at the end?

like image 825
Zerobu Avatar asked Mar 20 '10 18:03

Zerobu


People also ask

How do I get rid of trailing slash?

Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') . The replace method will remove the trailing slash from the string by replacing it with an empty string.

What is \s in Perl regex?

The Substitution Operator The substitution operator, s///, is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is − s/PATTERN/REPLACEMENT/; The PATTERN is the regular expression for the text that we are looking for.

Is there a trailing slash?

A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash.


1 Answers

With a regular expression do: $test =~ s/\/$//

Alternatively, if you're sure the last character is going to be a slash, you can use the chop function: chop $test

like image 96
miorel Avatar answered Oct 13 '22 19:10

miorel