-1

I have a string:

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

And I am doing this to see if str1 has str2 in it:

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

I am getting this : error: conversion from 'std::__cxx11::basic_string' to non-scalar type 'std::__cxx11::regex {aka std::__cxx11::basic_regex}' requested regex e = str2+"";

How to use the variable in my regex. Can someone please tell me where am I going wrong? TIA.

11
  • See stackoverflow.com/questions/662918/… Commented Feb 22, 2018 at 19:24
  • what types are str1, str2, hello and hel? How are they defined? Can you provide the complete code that produces the problem? Commented Feb 22, 2018 at 19:25
  • @WiktorStribiżew that question is for concatenation of string! Commented Feb 22, 2018 at 19:26
  • Also note that the regex hel* will match hello, but not for the reason you probably think. Commented Feb 22, 2018 at 19:26
  • 1
    In your real code does hello have quotes round it or is it another variable? Commented Feb 22, 2018 at 19:28

1 Answer 1

5

Look at reference about constructor of regex

template <class ST, class SA>
explicit basic_regex ( const basic_string<charT,ST,SA>& str, flag_type flags = 
 ECMAScript );

constructor is explicit so you should write

    regex e(str2+"*");

not

    regex e = str2+"*";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.