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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:54:04+00:00 2026-05-13T19:54:04+00:00

In the Advanced Regular Expresssion chapter in Mastering Perl , I have a broken

  • 0

In the “Advanced Regular Expresssion” chapter in Mastering Perl, I have a broken example for which I can’t figure out a nice fix. The example is perhaps trying to be too clever for its own good, but maybe someone can fix it for me. There could be a free copy of the book in it for working fixes. 🙂

In the section talking about lookarounds, I wanted to use a negative lookbehind to implement a commifying routine for numbers with fractional portions. The point was to use a negative lookbehind because that was the topic.

I stupidly did this:

$_ = '$1234.5678';
s/(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)/,/g;  # $1,234.5678

The (?<!\.\d) asserts that the bit before the (?=(?:\d\d\d)+\b) is not a decimal point and a digit.

The stupid thing is not trying hard enough to break it. By adding another digit to the end, there is now a group of three digits not preceded by a decimal point and a digit:

$_ = '$1234.56789';
s/(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)/,/g;  # $1,234.56,789

If lookbehinds could be variable width in Perl, this would have been really easy. But they can’t.

Note that it’s easy to do this without a negative lookbehind, but that’s not the point of the example. Is there a way to salvage this example?

  • 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-05-13T19:54:04+00:00Added an answer on May 13, 2026 at 7:54 pm

    I don’t think it’s possible without some form of variable-width look-behind. The addition of the \K assertion in 5.10 provides a way of faking variable-width positive look-behind. What we really need is variable-width negative look-behind but with a little creativity and a lot of ugliness we can make it work:

    use 5.010;
    $_ = '$1234567890.123456789';
    s/(?<!\.)(?:\b|\G)\d+?\K(?=(?:\d\d\d)+\b)/,/g;
    say;  # $1,234,567,890.123456789
    

    If there was ever a pattern that begged for the /x notation it’s this one:

    s/
      (?<!\.)        # Negative look-behind assertion; we don't want to match
                     # digits that come after the decimal point.
    
      (?:            # Begin a non-capturing group; the contents anchor the \d
                     # which follows so that the assertion above is applied at
                     # the correct position.
    
        \b           # Either a word boundary (the beginning of the number)...
    
        |            # or (because \b won't match at subsequent positions where
                     # a comma should go)...
    
        \G           # the position where the previous match left off.
    
      )              # End anchor grouping
    
      \d+?           # One or more digits, non-greedily so the match proceeds
                     # from left to right. A greedy match would proceed from
                     # right to left, the \G above wouldn't work, and only the
                     # rightmost comma would get placed.
    
      \K             # Keep the preceding stuff; used to fake variable-width
                     # look-behind
    
                     # <- This is what we match! (i.e. a position, no text)
    
      (?=            # Begin a positive look-ahead assertion
    
        (?:\d\d\d)+  # A multiple of three digits (3, 6, 9, etc.)
    
        \b           # A word (digit) boundary to anchor the triples at the
                     # end of the number.
    
      )              # End positive look-ahead assertion.
    /,/xg;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an advanced webpage (ASP.NET, C#), and a application which needs to be
Which are the most advanced frameworks and tools there are available for python for
The more and more advanced compilers, languages, and frameworks we have that either automate
I need to solve the following question which i can't get to work by
I know a few advanced ways, to change directories. pushd and popd (directory stack)
I'm using SQL Server 2008 with Advanced Services on my Vista Home Premium. I'd
I'm working on an 'advanced search' page on a site where you would enter
Anyway of invoking the Edit > Advanced > Format Document VS command automatically when
I'm looking to add an advanced search capability to my ASP.NET/SQL Server 2005 application.
I'm trying to learn advanced sql and how to use system queries (sql server).

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.