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?

2nd Sep 2024, 11:00 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
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"); }
2nd Sep 2024, 11:30 AM
Bob_Li
Bob_Li - avatar
+ 1
Thanks @Bob_Li
2nd Sep 2024, 1:06 PM
Ketan Lalcheta
Ketan Lalcheta - avatar