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 8127421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:31:31+00:00 2026-06-06T07:31:31+00:00

I need a one line gsub to replace all the non-digits in a string

  • 0

I need a one line gsub to replace all the non-digits in a string but only if the non-digits are not more than three and if the total length of the digits is 10

I have this which fits the first condition

p "0177/385490".gsub(/((\d+)\D?(\d+)\D?(\d+)\D?+(\d+))/,'\2\3\4\5') 
#=>"0177385490"

but when i try this the {10} check doesn’t work

p "0177/385490".gsub(/((\d+)\D?(\d+)\D?(\d+)\D?+(\d+)){10}/,'\2\3\4\5') 
#=>"0177/385490"

how to do this please ?

EDIT

i managed to to it like this, but how to do this in a oneline gsub ?

strings = [
   "0473/385 490",
   "0473/385490",
   "0473 38 54 90",
   "0473/385 4901"    #this one is't captured
 ]

 strings.each do |s|
   if /((\d+)\D?(\d+)\D?(\d+)\D?+(\d+))/ =~ s
     if "#{$2}#{$3}#{$4}#{$5}".length == 10
       puts "#{$2}#{$3}#{$4}#{$5}"
     end
   end
 end

EDIT: to show why it really needs to be a onle line gsub here my routine, there will be more replacements added

def cleanup text
  replacements = [
     {:pattern => /(04\d{2}) (\d{2}) (\d{2}) (\d{2})/, :replace_with => '\1\2\3\4'},
     {:pattern => /(0\d)(\/| |-)(\d{3}) (\d{2}) (\d{2})/, :replace_with => '\1\3\4\5'},
     {:pattern => /(\d{6} )(\d{3})-(\d{2})/, :replace_with => '\1\2 \3'},
     {:pattern => /(\d{2,4})\D?(\d{2,3})\D?(\d{2,3})/, :replace_with => '\1\2\3'}
  ].each{|replacement|text.gsub!(replacement[:pattern], replacement[:replace_with])}
  text
end
  • 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-06T07:31:33+00:00Added an answer on June 6, 2026 at 7:31 am

    Thanks everyone but i can’t use the answers because i can’t insert them in my routine (edited my answer to make that more clear). Found a sollution myself. I give everyone an upvote who had a one line solution as requested, now i still need to find a way to insert my block as a replacementpattern in the cleanup routine

    p "0177/3854901".gsub(/(\d+)\D?(\d+)\D?(\d+)\D?+(\d+)/){ |m| "#{$1}#{$2}#{$3}#{$4}".length==10 ? "#{$1}#{$2}#{$3}#{$4}":m} 
    #=> "0177/3854901" isn't replaced because it has 11 digits
    p "0177/385490".gsub(/(\d+)\D?(\d+)\D?(\d+)\D?+(\d+)/){ |m| "#{$1}#{$2}#{$3}#{$4}".length==10 ? "#{$1}#{$2}#{$3}#{$4}":m} 
    #=> "0177385490"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to take a sentence in that is all on one line with
I have a 1.2GB file that contains a one line string. What I need
I need to write a SELECT query in one line only that could potentially
I have some DataGrids with pretty standard one-line itemrenderers, but need to use a
I need a one line command for the below requirement. Search all the files
I have the following string which I need to get onto one line for
I need to replace for ' in all csv files in one directory. I
I need to create a batch file that reads a file with one line
I need to draw a line one inch long on any device given a
Need some help, please. I have a line of horizontal thumbnails loaded as ONE

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.