Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从regexp_match()获取结果_C++_Regex - Fatal编程技术网

C++ 如何从regexp_match()获取结果

C++ 如何从regexp_match()获取结果,c++,regex,C++,Regex,我的代码应该这样做: 输入:(x+y)=>(x*y) 输出:(x+y) regexp运行良好我在一个网站regexp101.com上测试了它们 如果我从regexp_match()中删除match参数,则代码将正常工作。 但它不会返回regexp结果。 我在运行此命令时遇到此错误: > g++ -std=c++11 main.cpp 错误 main.cpp:14:9:错误:调用“regex\u match”时没有匹配函数 if(正则表达式匹配(公式、匹配、第一大括号)){ ^~~~~~~

我的代码应该这样做:
输入:(x+y)=>(x*y)
输出:(x+y)
regexp运行良好我在一个网站regexp101.com上测试了它们 如果我从regexp_match()中删除match参数,则代码将正常工作。
但它不会返回regexp结果。 我在运行此命令时遇到此错误:

> g++ -std=c++11 main.cpp
错误

main.cpp:14:9:错误:调用“regex\u match”时没有匹配函数
if(正则表达式匹配(公式、匹配、第一大括号)){
^~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:6038:1:注意:
已忽略候选模板:为参数“\u BidirectionalIterator”推断出冲突类型
('std::u 1::basic_string'与'std::u 1::match_results')
正则表达式匹配(_BidirectionalIterator uu first,_BidirectionalIterator u last,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:6049:1:注意:
已忽略候选模板:无法将“const\u CharT*”与“std::\u 1::string”(aka)匹配
“基本字符串”)
正则表达式匹配(常量图*、匹配结果和,
^
/Applications/Xcode.app/Contents/Developer/toolschains/xcodefault.xctoolschain/usr/include/c++/v1/regex:6059:1:注意:
已忽略候选模板:第二个参数的推断类型“匹配结果”不匹配
已调整参数的类型“匹配结果”[with _ST=
std::_1::char\u traits,_SA=std::_1::分配器,_分配器=
std::_u1::分配器,_CharT=char,_Traits=
标准::_1::正则表达式特征]
正则表达式匹配(常量基本字符串和,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:6080:1:注意:
已忽略候选模板:无法将“const\u CharT*”与“std::\u 1::string”(aka)匹配
“基本字符串”)
正则表达式匹配(常量图表*\uuuuu str,常量基本正则表达式和,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:6089:1:注意:
已忽略候选模板:无法将“基本正则表达式”与“匹配结果”匹配
正则表达式匹配(常量基本字符串和,
^
/Applications/Xcode.app/Contents/Developer/toolschains/XcodeDefault.xctoolschain/usr/include/c++/v1/regex:5366:5:注意:
候选函数模板不可行:至少需要4个参数,但提供了3个
正则表达式匹配(_Bp,_Bp,匹配结果&,常量基本正则表达式&,
^
生成1个错误。

#包括
#包括
#包括
使用名称空间std;
int main(){
字符串公式;
库特公式;
cmatch匹配;
正则表达式firstBrace(“(\\()(\\w\\+)+(.\\)\\s?”);
if(正则表达式匹配(公式、匹配、第一大括号)){

cout您当前的代码无法编译,因为目标类型与匹配结果的类型不匹配。如果您将
string
作为输入传递,匹配的结果将存储在
smatch
中。当输入为
const char*
时,结果存储在
cmatch


您必须用
smatch

替换
cmatch
出现的问题,公式是
string
,因此使用
smatch
而不是
s
-string,
c
-char。另一个问题,我希望您知道
cin>..
读取到第一个空格。最好使用
getline(cin,公式)
@rafix07有效,我按照你告诉我的那样做了,顺便说一句,对于我不知道的解释,请发布一个答案,我将标记为正确answer@rafix07不要在评论部分回答。在回答部分回答。这会让人困惑吗?
main.cpp:14:9: error: no matching function for call to 'regex_match'
    if (regex_match(formula,match, firstBrace)){
        ^~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:6038:1: note: 
      candidate template ignored: deduced conflicting types for parameter '_BidirectionalIterator'
      ('std::__1::basic_string<char>' vs. 'std::__1::match_results<const char *,
      std::__1::allocator<std::__1::sub_match<const char *> > >')
regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:6049:1: note: 
      candidate template ignored: could not match 'const _CharT *' against 'std::__1::string' (aka
      'basic_string<char, char_traits<char>, allocator<char> >')
regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:6059:1: note: 
      candidate template ignored: deduced type 'match_results<typename basic_string<char,
      char_traits<char>, allocator<char> >::const_iterator, [...]>' of 2nd parameter does not match
      adjusted type 'match_results<const char *, [...]>' of argument [with _ST =
      std::__1::char_traits<char>, _SA = std::__1::allocator<char>, _Allocator =
      std::__1::allocator<std::__1::sub_match<const char *> >, _CharT = char, _Traits =
      std::__1::regex_traits<char>]
regex_match(const basic_string<_CharT, _ST, _SA>& __s,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:6080:1: note: 
      candidate template ignored: could not match 'const _CharT *' against 'std::__1::string' (aka
      'basic_string<char, char_traits<char>, allocator<char> >')
regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:6089:1: note: 
      candidate template ignored: could not match 'basic_regex' against 'match_results'
regex_match(const basic_string<_CharT, _ST, _SA>& __s,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:5366:5: note: 
      candidate function template not viable: requires at least 4 arguments, but 3 were provided
    regex_match(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&,
    ^
1 error generated.

#include <iostream>
#include <string>
#include <regex>
using namespace std;

int main() {
    string formula;

    cout << "Write down a formula" << endl;
    cin >> formula;
    cmatch match;

    regex firstBrace("(\\()(\\w\\+)+(.\\))\\s?");
    if (regex_match(formula,match, firstBrace)){
        cout << "String 'a' matches regular expression 'b' \n";
        cmatch mcopy (match);  // copy constructor
        for (unsigned i=0; i<mcopy.size(); ++i)
            cout << "match " << i << ": " << mcopy[i] << endl;
    }

    return 0;
}