-3

How can I separate the challenge string below into an array with a regex pattern?

For example:

$str = '{varA: "value, A", varB: "value:B", varC: "value C", varD: "value\"D\""}';

// remove both curved brackets
$trimStr = substr($str, 1, strlen($str)-1);
preg_match_all("/(.*?):\"(.*?)\",/sm", $trimStr, $m);  // would like the regex to ignore the ',' and double quote in the value string too

Into an array where

arr[0] = 'varA: "value, A"';
arr[1] = 'varB: "value:B"';
arr[2] = 'varC: "value C"';
arr[3] = 'varC: "value \"D\""';
6
  • 1
    What did you try? Commented Jan 4, 2021 at 12:47
  • 1
    Please share the regex pattern you've tried and explain how it fails (doesn't match anything, matches wrong value...). Commented Jan 4, 2021 at 12:49
  • have updated the questions with what i got at the moment Commented Jan 4, 2021 at 13:04
  • 1
    Not sure why the question was closed now that it has full details. Commented Jan 4, 2021 at 14:00
  • The string $str contains a Javascript object notation. It's not JSON! I haven't found a good solution to convert such a notation into a PHP array. Commented Jan 4, 2021 at 14:24

3 Answers 3

1

In your example it seems like you don't need to use regex at all, just remove the curly brackets and explode your string into an array. Since your elements are already comma separated, just use the correct delimiter.

$arr = explode(', ', $str);

However if you insist on using regex, here's an expression that will match your elements:

var\w+\:\s\".+?(?=\,|})
Sign up to request clarification or add additional context in comments.

3 Comments

how could i able to escape the comma and double quote in the value?
The explode works only if there is no comma in the values. However, it would break if there exist , in the values.
@user3783243 thanks for the tips. Will update the ques
0

You need to give optional spacing between the : and " and you need to change delimiter to , or end of line.

(.*?):\h*\"(.*?)\"(?:,|$)

https://regex101.com/r/aAyrQA/1/

or:

\{?(.*?):\h*\"(.*?)\"(?:,|\}|$)

and you can remove the $trimStr = substr($str, 1, strlen($str)-1);.

full PHP example:

preg_match_all('/\{?(.*?):\h*\"(.*?)\"(?:,|\}|$)/', '{varA: "value, A", varB: "value:B", varC: "value C", varD: "value\"D\""}', $matches);
foreach($matches[1] as $key => $match) {
    $arr[] = $match . ' :"' . $matches[2][$key] . '"';
}

https://3v4l.org/0nJqW

Comments

0

/(.*?):\"(.*?)\",/sm => /{?((.*?): ?"(.*?)")[,}]/ try regex

PHP code:

$str = '{varA: "value A", varB: "value:B", varC: "value C", varD: "value"D""}';
$pattern = '/{?((.*?): ?"(.*?)")[,}]/';
$arr = [];

preg_match_all($pattern, $str, $matches);
foreach($matches[1] as $match){
  $arr[] = $match;
}
print_r($arr);

Comments

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.