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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:14:31+00:00 2026-05-29T11:14:31+00:00

I need a fairly complex regex to accomplish the following: > replace numbers in

  • 0

I need a fairly complex regex to accomplish the following:

> replace numbers in a string, i.e. 700, 12.43 by a label (format: {NUMBER:xx})
> ignore: when number is between {braces}, i.e. {7}, {7th}
> ignore: when any character is attached to number, i.e. G3, 7x, 1/2
> except: when
          > preceded by $, i.e. $840
          > followed by .!?:, i.e. 33! 45.65?  4...

Taken all together:

Buy 4 {5} G3 Mac computers for 80% at $600 or 2 for 1/2 price: 200... 
dollar. Twice - 2x - as cheap!

Desired output:

Buy {NUMBER:4} {5} G3 Mac computers for 80% at 
$ {NUMBER:600} or {NUMBER:2} for 1/2 price: 
{$NUMBER:200} dollar. Twice - 2x - as cheap!

I now have this:

preg_replace("/(?<!{)(?>[0-9]+(?:\.[0-9]+)?)(?!})/", " {NUMBER:$0} ", $string);

which outputs:

Buy {NUMBER:4} {5} G {NUMBER:3} Mac computers for {NUMBER:80} % at 
$ {NUMBER:600} or {NUMBER:2} for {NUMBER:1} / {NUMBER:2} price: 
{NUMBER:200} ... dollar. Twice - {NUMBER:2} x - as cheap!

In other words: ignoring exceptions aren’t working yet, and I don’t know how to properly implement it. Who does and can help me out?

  • 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-29T11:14:32+00:00Added an answer on May 29, 2026 at 11:14 am

    This works for your test cases and follows your rules, assuming that braces are correctly matched and unnested:

    $result = preg_replace(
        '/(?<!\{)        # Assert no preceding {
        (?<![^\s$])      # Assert no preceding non-whitespace except $
        \b               # Match start of number
        (\d+(?:\.\d+)?+) # Match number (optional decimal part)
        \b               # Match end of number
        (?![^{}]*\})     # Assert that next brace is not a closing brace
        (?![^\s.!?,])    # Assert no following non-whitespace except .!?,
        /x', 
        '{NUMBER:\1}', $string);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly complex SQL query, where we'll need to return a number
We are building a fairly complex application that we need to be able to
I have a fairly complex scenario and I need to ensure items I have
I've got a fairly complex XML document, where I need to remove specific nodes
We need to produce a fairly complex dynamic query builder for retrieving reports on
I am working with a fairly complex third-party XSD format - XSDs refer to
I have a fairly complex data model with approximately 10 entities. Some need to
I have a fairly long and complex HTML document, and I need to find
I need to use some fairly complex Jquery plugins that aren't just a single
I'm finding the need to lay out a fairly complex UI manually, by giving

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.