首页 > 解决方案 > 无法使用 regex_search 匹配大括号

问题描述

我正在尝试使用 smatch 捕获大括号的位置:

smatch l, r, m;
auto closing = regex_search(ns, r, regex("\}"));

这段代码给了我运行时错误:

libc++abi: terminating with uncaught exception of type std::__1::regex_error: The parser did not consume the entire regular expression.

我也在这里捕获:

regex_search(s, m, regex("\"menu\"\s*:\s*\{"))

这给了我另一个:

libc++abi: terminating with uncaught exception of type std::__1::regex_error: One of *?+{ was not preceded by a valid regular expression.

我的文字是这样的:

"{  \"kvakushka\": {    \"popup\": {      \"menuitem\": [        \"mur\",        \"koshka\",        {          \"mur\": \"kva\",          \"vas\": \"gas\"        }      ]    }  },  \"menu\": {    \"id\": \"file\",    \"value\": \"File\",    \"popup\": {      \"menuitem\": [        \"file\", \"vasya\"      ]    },    \"menuitem\": [      {        \"value\": \"Font\"      },      {        \"value\": \"Size\"      }    ]  },  \"wisp\": {    \"popup\": {      \"menuitem\": [        \"mur\"        :        5,        \"var\"        :        10      ]    }  }}"

标签: c++regex

解决方案


推荐阅读