0

Help me to split these kind of string :

Example:

string to split : "{'day': 1, 'count': 100}, {'day': 2, 'count': 100}"

expected output : [ {'day': 1, 'count': 100}, {'day': 2, 'count': 100} ]

using namespace std;;
#include<string>
#include<regex>
#include<iostream>
int main()
{
  string s1("{asd, dsf},{fsdfsdf, sdff}");
  regex e("/{([^}]*)}/");

  cout << s1 << std::endl;

  sregex_iterator iter(s1.begin(), s1.end(), e);
  sregex_iterator end;

  while(iter != end)
  {
    cout << (*iter)[i] << std::endl'
    ++iter;
  }
 }

this code fails for regex mentioned in the https://stackoverflow.com/a/413077/11017988

indev
  • 125
  • 2
  • 8
  • Is this just an extreme case, or it'll always be some output in a JSON format? – Coral Kashri May 30 '20 at 11:06
  • it is always an output of JSON.stringify of an array – indev May 30 '20 at 11:09
  • @Wiktor Stribiżew https://stackoverflow.com/questions/413071/regex-to-get-string-between-curly-braces doesn't solve this case – indev May 30 '20 at 11:13
  • Regex for strings between curly braces is [here](https://stackoverflow.com/a/413077/3832970). Solves the issue. – Wiktor Stribiżew May 30 '20 at 11:16
  • @WiktorStribiżew can you please share an example cpp snippet to implement that regex, 'cause mine gives **Abort signal from abort(3) (SIGABRT)** – indev May 30 '20 at 11:28
  • https://stackoverflow.com/questions/11627440/regex-c-extract-substring, https://stackoverflow.com/questions/32553593/c-regex-extract-all-substrings-using-regex-search etc. – Wiktor Stribiżew May 30 '20 at 11:30
  • @WiktorStribiżew those snippets also fails for the regex which was mentioned as a solution by you, that's why I have asked a new question – indev May 30 '20 at 11:48
  • Show the code that fails, add the snippet and link to a demo into the question. – Wiktor Stribiżew May 30 '20 at 11:49
  • 1
    Regex `regex e ("\\{[a-z':, 0-9]+\\}");` will also help. See https://stackoverflow.com/a/62102349/7016115 – Shudipta Sharma May 30 '20 at 12:25

0 Answers0