Home > Net >  set empty std::regex
set empty std::regex

Time:04-06

How to create a regex from an input argument? If the argument is empty or undefined no regex should be set

int main(int argc, char* argv[]){
    std::string arg_name;
    std::string arg_filter;
    
    std::cmatch rem;
    std::regex re;
    
    //  Match cmd name
    if(argc > 1){
        arg_name = std::string(argv[1]);
    }
    
    //  Match cmd filter
    if(argc > 2){
        arg_filter = std::string(argv[2]);
        
        if(!arg_filter.empty()){
            const std::regex re(arg_filter);
        }
    }
    
    std::string cmd = "test";
    if(!arg_filter.empty()){
        if(!std::regex_search(cmd, rem, re)){
            continue;
        }
    }
    
    return 0;
}

error

# g   -rdynamic -O2 -std=c  17 proc.cpp -o proc
proc.cpp: In function ‘int main(int, char**)’:
proc.cpp:49:37: error: no matching function for call to ‘regex_search(std::__cxx11::string&, std::__cxx11::cmatch&, std::__cxx11::regex&)’
   if(!std::regex_search(cmd, rem, re)){
                                     ^
In file included from /usr/include/c  /8/regex:62,
                 from proc.cpp:12:
/usr/include/c  /8/bits/regex.h:2183:5: note: candidate: ‘template<class _Bi_iter, class _Alloc, class _Ch_type, class _Rx_traits> bool std::regex_search(_Bi_iter, _Bi_iter, std::__cxx11::match_results<_BiIter, _Alloc>&, const std::__cxx11::basic_regex<_CharT, _TraitsT>&, std::regex_constants::match_flag_type)’
     regex_search(_Bi_iter __s, _Bi_iter __e,
     ^~~~~~~~~~~~

update

int main(int argc, char* argv[]){
    bool use_name           = false;
    std::string arg_name;
    
    bool use_filter         = false;
    std::string arg_filter;
    
    std::smatch rem;
    std::regex re;
    
    //  Match cmd name
    if(argc > 1){
        use_name            = true;
        arg_name            = std::string(argv[1]);
    }
    
    //  Match cmd filter
    if(argc > 2){
        use_filter          = true;
        arg_filter          = std::string(argv[2]);
        const std::regex re(arg_filter);
    }
    
    std::string cmd = "php /var/www/php/cronjob.php listen_websockets";
    std::cout << "Input: " << cmd << std::endl;
    if(use_filter){
        std::cout << "Filter used!" << std::endl;
        
        if(std::regex_search(cmd, rem, re)){
            std::cout << "Match!" << std::endl;
        }
        else{
            std::cout << "No match!" << std::endl;
        }
    }
    return 0;
}

The program is not matching anything..

# ./proc php www
Input: php /var/www/php/cronjob.php listen_websockets
Filter used!
No match!

CodePudding user response:

As mentioned in the comments, you should use smatch instead of cmatch since you are using std::string. Also, you have created 2 separate re variables. You only want one.

int main(int argc, char **argv)
{
    //  Match cmd name
    std::string arg_name;
    if (argc > 1){
        arg_name = std::string{argv[1]};
    }

    const std::regex re{argc > 2 ? argv[2] : ""};
    
    //  Match cmd filter
    if (argc > 2){
        std::string arg_filter = std::string{argv[2]};
        if (!arg_filter.empty()){
            std::string cmd{"test"};
            std::smatch rem;
            if (std::regex_search(cmd, rem, re)){
                std::cout << "is match\n";
            }
            else {
                std::cout << "no match\n";
            }
        }
    }

    return 0;
}
  • Related