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

  • SEARCH
  • Home
  • 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 8809531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:53:55+00:00 2026-06-14T02:53:55+00:00

Please have a look at my mind-breaker. I’d stuck in shrinking with regex some

  • 0

Please have a look at my mind-breaker.

I’d stuck in shrinking with regex some long path, like this:

/12345/123456/1234/123/12/1/1234567/13245678/123456789/1234567890

I’d like to transform this path to the following form:

/123/123/123/123/12/1/123/123/123/123

each “directory” in a path abbreviates to only 3 first characters

LONG_PATH="/12345/123456/1234/123/12/1/1234567/13245678/123456789/1234567890"
perl -pe "s#/(.{1,3})[^/]*?(/|$)#/\1\2#g" <<<$LONG_PATH

/123/123456/123/123/12//1234567/132/123456789/123

sed -E "s#/(.{1,3})[^/]*?(/|$)#/\1\2#g" <<<$LONG_PATH

/123/123456/123/123/12//1234567/132/123456789/123

I have tried also:

perl -pe "s,/(.)(.)?(.)?[^/]*+,/\1\2\3,g" <<<$LONG_PATH
/123/123/123/123/12//123/132/123/123

and many another, no “luck” – I still have no idea about.

Please point me a right way to success.

  • 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-06-14T02:53:56+00:00Added an answer on June 14, 2026 at 2:53 am

    Match up to three non-slash characters and capture them. Then match the rest until the next slash. Replace by the capture:

    "s#(/[^/]{3})[^/]*#\1#g"
    

    There is no need for ungreediness or anything here, because the negated character class is mutually exclusive with the / or $.

    EDIT: Although you seem to know this I should probably clarify for future visitors that this will work with either perl -pe... or sed -E... as you have used it in your question. The regex could also be used as is with sed -r.... If you leave out the -E or -r option, then (as usual) you will need to escape both the parentheses and curly brackets:

    sed "s#\(/[^/]\{3\}\)[^/]*#\1#g" filename
    

    Note also as ikegami points out that in Perl you should rather use $1 in the replacement than \1.

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

Sidebar

Related Questions

Please have a look at the following code: $(#saveButton).click(function(){ $this = $(#tableData).find(input:checked).parent().parent(); tea =
Please have a look at the following machine code ‎0111001101110100011100100110010101110011011100110110010101100100 This means something. I
Please have a look at this form that I am trying to design with
Please have a look at this page www.pixeli.ca/issue. I have begun making a page
Please have a look at this demo page that I cooked up a while
This is the code I have, please look at it before you read the
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
Please have a look at the following code package Euler; import java.util.ArrayList; import java.util.List;
Please have a look at the code below: import string from collections import defaultdict

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.