0
Regex of two substrings in C++
Hi Refer code below: ///////////////////////////////////////////////// string name = "checkRealEditorWithCheckbox"; if (name.find("check") != std::string::npos && name.find("Editor") != std::string::npos ) cout << "Matched\n"; else cout << "Not matched\n"; /////////////////////////////////////////// Above code works fine as I am trying to find whether string has check and Editor both. Due to this, I have to do find operation twice which I am trying to avoid and want to do once only. Is there a way to do so? Can regex help here? If yes, how?
2 Answers
+ 4
// Be aware that regex is notoriously slow in C++. Your original code is faster.
#include <iostream>
#include <regex>
using namespace std;
int main() {
string name = "checkRealEditorWithCheckbox";
cout << (regex_search(name,regex("check.*Editor")) ?
"Matched\n" : "Not matched\n");
}
+ 1
Thanks @Bob_Li