如何从 regexp_match() 中获取结果

How to get result from regexp_match()

本文关键字:获取 结果 match regexp      更新时间:2023-10-16

我的代码应该这样做:
输入:(x+y( => (x*y(
输出:(x+y(
正则表达式运行良好 我在网站上测试了它们 regexp101.com 如果我从 regexp_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;
}

如何从正则表达式中获取结果?

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

您必须将cmatch出现的次数替换为smatch