我的代码应执行以下操作:
输入:(x + y)=>(x * y)
输出:(x + y)
regexp运作良好,我在regexp101.com网站上对其进行了测试
如果我从regexp_match()中删除match参数,则代码有效。
但是它不返回正则表达式结果。
运行此命令时出现此错误:
> g++ -std=c++11 main.cpp
错误
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;
}
如何从正则表达式中得到结果?
答案 0 :(得分:1)
您当前的代码无法编译,因为目标类型与 match 结果类型不匹配。如果您通过string
作为输入,则匹配的结果将存储在smatch
中。输入为const char*
时,结果存储在cmatch
中。
您必须将cmatch
个事件替换为smatch
。