Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

433
Views
GSUB replace 3 or more repeating characters

A little help would be appreciated with a gsub regex in ruby. I need to replace 3 or more forward slashes "//////" with just 2 forward slashes "//" in a string of text. However, a single forward slash and double forward slashes should be skipped and left as is.

my data looks like this jeep/grand cherokee////////hyundai/////harley davidson//bmw and should be converted to jeep/grand cherokee//hyundai//harley davidson//bmw

I don't have much experience with using gsub regex's, here's a few things I've tried but they either strip out all forward slashes or add in too many.

  vehicles = vehicles.gsub(/[\/\\1{3,}]/, "")

  vehicles = vehicles.gsub(/[\/\2+]/, "//")

  vehicles = vehicles.gsub(/[\/{3,}]/,"//")
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

When you enclose the whole pattern inside square brackets, you make it match a single char.

Your regexps mean:

  • [\/\\1{3,}] - a single char, /, \, 1, {, 3, , or }
  • [\/\2+] - /, \u0002 char or +
  • [\/{3,}] - /, {, 3, , or }

You can use

s.gsub(/\/{3,}/, '//')

See the Ruby demo online.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!