如何从 regexp_match() 中获取结果
How to get result from regexp_match()
我的代码应该这样做:
输入:(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
。
相关文章:
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- C++ 获取函数在常量引用中按值返回的结果
- 从返回 std::optional of std::vector 的函数中获取结果到调用方
- 从 C++ 中的 URL 获取结果
- 如何从主.cpp中的cpp文件中获取功能结果?
- 如何从 regexp_match() 中获取结果
- 无法在 WinRT 中获取异步操作结果 (Windows::Foundation::IAsyncOperating 接口
- mysql++(mysqlpp):如何在迭代之前通过UseQueryResult使用fetch_row获取结果中的行数
- QDialog exec() 并获取结果值
- 从非线程阻塞消息框获取结果
- 如何使用 QueryPerformanceCounter 在几秒钟内获取结果
- C++执行命令行并获取结果
- C++模板方法和获取结果的设计
- 如何从服务器获取结果
- 从返回类型的函数中获取结果'void'而结果变量是输入参数之一 - C++
- 从具有可变数量模板化参数的模板化类中的特定基类获取结果
- 取消引用无效指针,然后获取结果的地址
- 在Windows上执行进程并获取结果
- 用c++运行一个进程,过滤输出,获取结果代码,同时获取系统错误
- 通过usb从控制器获取结果失败