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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:58:43+00:00 2026-06-13T13:58:43+00:00

The following works in Ruby for commafication (adding , to a number, so 12345

  • 0

The following works in Ruby for commafication (adding , to a number, so 12345 becomes 12,345)

def r(s)
  s.gsub(/(?<=\d)(?=(\d\d\d)+\b)/, ",")
end

s = ""
1.upto(20) do |i|
  s += (i % 10).to_s
  puts r(s)
end

But I wonder why the variations r2 and r3 won’t work?

def r2(s)
  s.gsub(/(?<=\d)(?=(\d\d\d)+)\b/, ",")
end

def r3(s)
  s.gsub(/(?<=\d)(?=\d\d\d)+\b/, ",")
end

Nothing is modified at all, and I would think that 1234 does match (?<=\d)(?=(\d\d\d)+)\b so it is a bit strange. (I tried it using Perl as well, so it is not peculiar to Ruby).

Update: The following is the output for r, while for r2 and r3, no , is added at all:

1
12
123
1,234
12,345
123,456
1,234,567
12,345,678
123,456,789
1,234,567,890
12,345,678,901
123,456,789,012
1,234,567,890,123
12,345,678,901,234
123,456,789,012,345
1,234,567,890,123,456
12,345,678,901,234,567
123,456,789,012,345,678
1,234,567,890,123,456,789
12,345,678,901,234,567,890
  • 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-13T13:58:44+00:00Added an answer on June 13, 2026 at 1:58 pm

    Well, in r2, your lookahead is saying that the next three characters must be digits, but then you immediately try to match a word boundary. They are mutually exclusive.

    In r3, you are repeating the lookahead one or more times, but, being a lookahead, this is nonsense. You are repeating “the next three characters must be digits” over and over, but they either will be or won’t be. Stating it more than once is non-sense. And you still have the problem with the word boundary.

    A lookahead is like a peek function on a stack. It doesn’t move the pointer forward because it doesn’t consume anything. It matches a position (think of that as the space in-between characters). So your lookahead is matching the position where three digits follow. But then the next statement (the \b) is matching a position where the character on the left is a word character (typically [a-zA-Z0-9_] or something like that) and the character to the right is not (whitespace, a period, etc.) or vice versa. Since the previous lookbehind requires that there be digit preceding the position, and the lookahead requires a sequence of digits, then it is impossible to ever have a word boundary at the defined position.

    Example

    The following regex will always fail:

    ^(?=\d\d\d)\d\d\b
    

    The ^ says that the match must start at the beginning of the input. The lookahead asserts that the next three characters must be digits (but does not consume them). The following expression says that the next two characters must be digits (and consumes them, moving the pointer forward), followed by a non-digit (the word boundary). But this violates the lookahead which required the next three characters must be digits. Thus, the match fails.

    See: http://www.regular-expressions.info/lookaround.html

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

Sidebar

Related Questions

Consider the following sample ruby class class User def hello puts hello end end
The following code works fine: class Float def round self.format.to_f end def format %.2f
I wrote the following Ruby code: def myItems if @item1 yield @item1 end if
I have the following bit of ruby code which works fine require 'WIN32OLE' excel
The following command works in Ruby 1.9.3p194 > %w(a b c).grep(/a/) { |v| v.upcase
Consider the following code in ruby, assume I called prestart from somewhere: def tester(process_name,
Following works fine in shell: >>> from django.contrib.auth.models import User >>> user=User.objects.get(pk=1) >>> user.first_name
The following works great (thanks to gracious assistance by this community!) Function RangeToArrayToRange(inputRange as
The following works in current Firefox (PDF loads), but not in current IE (page
The following works: public Date getDate() { return date; } and produces something like:

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.