Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7517513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:28:35+00:00 2026-05-30T01:28:35+00:00

In source code I have to read (not from me) I found a regex

  • 0

In source code I have to read (not from me) I found a regex that is not working as expected (or at least with what it does the code is not doing what it should).

The Idea of the regex is to make sure the string it processes has exactly one slash at the end afterwards.

So here we go:

$string =~ s/\/*$/\//g; # Add a trailing slash

I read this as “find any number of slashes at the end of the string (0 included) and replace them with a single slash”. But this is not what happens.

The regex doubles an/multiple existing trailing slash(es). So I end up with two slashes if there already is one or more. If there is none I get one (as it would be the idea).

I can reproduce it with Regex tester for example. Just enter http://example.com/foo/ (and vary it with different slash count at the end) in the top box, the regex in the second, “/” as the replace pattern and hit “send”.

It seems to revolve around the regex also replacing “nothing” (or the line ending?) with a slash (so it replaces “nothing” with one slash and the existing slash with one slash which makes up for the two slashes I see…).

I used

$string =~ s/\/{1,0}$/\//g; # Add a trailing slash

also but this only works if there is at least one slash from the beginning.

EDIT

Just in case anyone is wondering:

$string =~ s/\/*\z/\//g; # Add a trailing slash

produces the same thing.

And here is a direct link to the test page

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:28:37+00:00Added an answer on May 30, 2026 at 1:28 am

    The problem is the /g modifier, which says to replace all occurrences. When you have a slash at the end of the string, Perl makes two replacements. First, it replaces the slash with another slash. Then (because $ is a zero-width assertion, and does not consume any characters) it replaces the empty string at the end with a slash. Remove the /g and it will work, because only the first match will be replaced:

    $string =~ s!/*$!/!; # Add a trailing slash
    

    (I also changed the delimiter to make it more readable, although that had nothing to do with your problem.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this source code from 2001 that I would like to compile. It
I have used an open source code from CodeProject to read email from incoming
I have an executable that defaults to 32-bit. It doesn't have source code and
I have seen sample source code around that uses different ways of releasing/dealloc'ing objects,
I have a source code that is needed to be converted by creating classes,
I have written some code that makes use of an open source library to
I'm building a python application from some source code I've found Here I've managed
From what I've read, code obfuscation isn't really that hard to crack, and it
What do you insert into Python source code to have it break into pdb
In my C# source code I may have declared integers as: int i =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.