Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: bug in regexp engine?

by wog (Curate)
on Sep 29, 2001 at 06:22 UTC ( [id://115570]=note: print w/replies, xml ) Need Help??


in reply to bug in regexp engine?

You must remember that (?=...) is a zero-width assertion. Thus (\d{3})(?=(\d{3})+)$ tries to first match three digits, then sees if the text after where it is in the string matches (\d{3})+ without moving further in the string and then sees it can match an end of string or newline after its current location in the string.

Replies are listed 'Best First'.
Re: Re: bug in regexp engine?
by stefp (Vicar) on Sep 29, 2001 at 22:44 UTC
    wog is right. My mistake was to put the right anchor outside the zero width look-ahead assertion. The correct substitution code is:

     s/(\d{1,3}?)(?=(\d{3})+$)/$1_/g;

    The lookahead makes sure that the number of digits before each underscore we insert is a multiple of 3

    The lookahead: (?=(\d{3})+$)
    I needed an extra set of parenthesis to fool Perl because the regexp parser barks if there is two quantifiers in a row, which is perfectly legitimate here.

    There is a general lesson to be learned here: unchecked idiotism are for idiots. So much for me :)

    When dealing with new material (here regexp assertion that I have not used much), one must learn to reassess idiotisms that may not work in a new larger context. Here, I used the idotism: force the match to the end of the string => add a $ at the the very end of the regexp. It did not work here because I wanted the lookahead to match to the end of the string.

    Compare the previous code with the easy way

    -- stefp

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://115570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found