1

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

2 Answers 2

2

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

Try:

$jsonToken = '@(?:false|true|null'
      .'|[\\}]'
      .'|'.$varName
      .'|'.$func
      .'|'.$number
      .'|'.$string
      .')@';
Sign up to request clarification or add additional context in comments.

Comments

2

In preg, the delimiter is needed in the pattern, e.g. you use it as

preg_match_all('#[a-z]+#i', ....);   // # is the delimiter, i means case-insensitive.

Try to add them and see if there're still any errors.

1 Comment

Thanks for your response, both worked, so I went by who answered first, sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.