在Regex CPP中使用变量

using a variable in regex cpp

本文关键字:变量 Regex CPP      更新时间:2023-10-16

我有一个字符串:

string str1= "hello"; 
string str2="hel"; 

我正在这样做以查看str1是否有str2:

regex e = str2+"*"
 bool x=regex_match(str1,e); 
cout<<x; 

我得到的:错误:从'std :: __ cxx11 :: basic_string'到非降低类型的'";

如何在我的正则使用变量。有人可以告诉我我要去哪里?tia。

查看有关 regex

构造函数的参考
template <class ST, class SA>
explicit basic_regex ( const basic_string<charT,ST,SA>& str, flag_type flags = 
 ECMAScript );

构造函数是显式的,因此您应该写

    regex e(str2+"*");

不是

    regex e = str2+"*";