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

The Archive Base Latest Questions

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

I am up to my neck in regular expressions, and I have this regular

  • 0

I am up to my neck in regular expressions, and I have this regular expression that works in javascript (and flash) that I just can’t get working in PHP

Here it is:

  var number
      = '(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)';
  var oneChar = '(?:[^\\0-\\x08\\x0a-\\x1f\"\\\\]'
      + '|\\\\(?:[\"/\\\\bfnrt]|u[0-9A-Fa-f]{4}))';
  var str = '(?:\"' + oneChar + '*\")';
  var varName = '\\$(?:' + oneChar + '[^ ,]*)';
  var func = '(?:{[ ]*' + oneChar + '[^ ]*)';
  // Will match a value in a well-formed JSON file.
  // If the input is not well-formed, may match strangely, but not in an unsafe
  // way.
  // Since this only matches value tokens, it does not match whitespace, colons,
  // or commas.
  var jsonToken = new RegExp(
      '(?:false|true|null'
      +'|[\\}]'
      + '|' + varName
      + '|' + func
      + '|' + number
      + '|' + str
      + ')', 'g');

If you want it fully assembled here it is:

/(?:false|true|null|[\}]|\$(?:(?:[^\0-\x08\x0a-\x1f"\\]|\\(?:["/\\bfnrt]|u[0-9A-Fa-f]{4}))[^ ,]*)|(?:{[ ]*(?:[^\0-\x08\x0a-\x1f"\\]|\\(?:["/\\bfnrt]|u[0-9A-Fa-f]{4}))[^ ]*)|(?:-?\b(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b)|(?:"(?:[^\0-\x08\x0a-\x1f"\\]|\\(?:["/\\bfnrt]|u[0-9A-Fa-f]{4}))*"))/g

Interestingly enough, its very similar to JSON.

I need this regular expression to work in PHP…

Here’s what I have in PHP:

    $number = '(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)';
    $oneChar = '(?:[^\\0-\\x08\\x0a-\\x1f\"\\\\]|\\\\(?:[\"/\\\\bfnrt]|u[0-9A-Fa-f]{4}))';
    $string = '(?:\"'.$oneChar.'*\")';
    $varName = '\\$(?:'.$oneChar.'[^ ,]*)';
    $func = '(?:{[ ]*'.$oneChar.'[^ ]*)';

    $jsonToken = '(?:false|true|null'
      .'|[\\}]'
      .'|'.$varName
      .'|'.$func
      .'|'.$number
      .'|'.$string
      .')';

    echo $jsonToken;

    preg_match_all($jsonToken, $content, $out);

    return $out;

Here’s what happens if I try using preg_match_all():

Warning: preg_match_all()
[function.preg-match-all]: Compilation
failed: nothing to repeat at offset 0
on line 88

Any help would be much appreciated!

Thanks,
Matt

  • 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-14T00:27:31+00:00Added an answer on May 14, 2026 at 12:27 am

    I guess this is happening because you don’t have your regex between the delimiters.

    Try:

    $jsonToken = '@(?:false|true|null'
          .'|[\\}]'
          .'|'.$varName
          .'|'.$func
          .'|'.$number
          .'|'.$string
          .')@';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Generally We have some business logic that is causing a bottle neck within a
I have a .NET 4 C# service that is using the TPL libraries for
I have pin pointed a bottle neck in my application, it seems to me
I write a lot of my code in static methods/classes/variables so that they can
I have a time dependent script I am working on and used microtime() to
I have a webservice that is in much need of some optimization. It's on
right now i have a bottle neck in my program I'm trying to write.
So you have a third-party web service that likes to abuse XML and return
I have a query that takes about 3 minutes to run. The Query is
So I have noticed this while using a few differnt languages on Windows XP,

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.