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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:30:01+00:00 2026-06-04T01:30:01+00:00

Given the following indented text: two spaces four six non-leading spaces I’d like to

  • 0

Given the following indented text:

  two spaces
    four
      six
non-leading    spaces

I’d like to convert every 2 leading spaces to a tab, essentially converting from soft tabs to hard tabs. I’m looking for the following result (using an ‘x’ instead of “\t”):

xtwo spaces
xxfour
xxxsix
non-leading  spaces

What is the most efficient or eloquent way to do this in ruby?

What I have so far seems to be working, but it doesn’t feel right.

input.gsub!(/^ {2}/,"x")
res = []
input.split(/\n/).each do |line|
  while line =~ /^x+ {2}/
    line.gsub!(/^(x+) {2}/,"\\1x")
  end
  res << line
end
puts res.join("\n")

I noticed the answer using sed and \G:

perl -pe '1 while s/\G {2}/\t/gc' input.txt >output.txt

But I can’t figure out how to mimic the pattern in Ruby. This is as far as I got:

rep = 1
while input =~ /^x* {2}/ && rep < 10
  input.gsub!(/\G {2}/,"x")
  rep += 1
end
puts input
  • 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-04T01:30:02+00:00Added an answer on June 4, 2026 at 1:30 am

    Whats wrong with using (?:^ {2})|\G {2} in multi-line mode?

    The first match will always be at the beginning of the line,
    then \G will match succesively right next to that, or the match
    will fail. The next match will always be the beginning of the line.. repeats.

    In Perl its $str =~ s/(?:^ {2})|\G {2}/x/mg; or $str =~ s/(?:^ {2})|\G {2}/\t/mg;

    Ruby http://ideone.com/oZ4Os

    input.gsub!(/(?:^ {2})|\G {2}/m,"x")
    

    Edit: Of course the anchors can be factored out and put into an alternation
    http://ideone.com/1oDOJ

    input.gsub!(/(?:^|\G) {2}/m,"x")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given following array: var arr = [undefined, undefined, 2, 5, undefined, undefined]; I'd like
Given the following scenario, I would like to know if functionOneLock releases itself before
Given the following function: function process_pipes(text) { split(text,recs,|); for (field in recs){ printf (|%s|\n,
Given the following text in Vim: [2] [3] [4] I want to perform a
I should convert some code from VB to C#. Given following lines of VB
Given the following tables: create table index(name text, docid int); create virtual table docs
Given following snippet in a view: <style type=text/css> input[type=text] { width: <%: Model.CmsConfiguration.cms_form_width %>px;
I have a string that represents a non indented XML that I would like
Given following Ruby statements: (Read input and store each word in array removing spaces
Given a textarea, similar to StackOverflow, I'd like to wrap code (indented by 4

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.