Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::regex and nlohmann::json value #724

Closed
jm130794 opened this issue Sep 3, 2017 · 3 comments
Closed

std::regex and nlohmann::json value #724

jm130794 opened this issue Sep 3, 2017 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@jm130794
Copy link

jm130794 commented Sep 3, 2017

Hello,

I want to use std::regex with a nlohmann::json value but I have somme problems.

My C++ file :

// g++ -std=c++14 moderncpp_json.cpp -o moderncpp_json
#include <iostream>
#include <regex>
#include "json.hpp"
 
int main(int argc, char *argv[])
{
    using json = nlohmann::json;

    json j = R"({"name": "jmchoulet"})"_json;

    std::regex  r{R"(^(.+)$)"};
    std::smatch m;

    //-- works but need to create a std::string variable !!!                         
    //const std::string value = j["name"].get<std::string>();
    //if (std::regex_match(value, m, r))

    if (std::regex_match(j["name"].get<std::string>(), m, r))
    {   
        std::string s;

        if (m.size())
        {
            std::string tmp = m[1];
            s += "m[1]: " + tmp;
            std::cout << s << std::endl;
        }
    }

    return 0;
}

Compiler g++ output :

$ g++ -std=c++14 moderncpp_json.cpp -o moderncpp_json
moderncpp_json.cpp: In function ‘int main(int, char**)’:
moderncpp_json.cpp:19:60: error: use of deleted function ‘bool std::regex_match(const std::__cxx11::basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&, std::__cxx11::match_results<typename std::__cxx11::basic_string<_Ch_type, _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&, const std::__cxx11::basic_regex<_Ch_type, _Rx_traits>&, std::regex_constants::match_flag_type) [with _Ch_traits = std::char_traits<char>; _Ch_alloc = std::allocator<char>; _Alloc = std::allocator<std::__cxx11::sub_match<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> > > >; _Ch_type = char; _Rx_traits = std::__cxx11::regex_traits<char>; typename std::__cxx11::basic_string<_Ch_type, _Ch_traits, _Ch_alloc>::const_iterator = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >]’
     if (std::regex_match(j["name"].get<std::string>(), m, r))
                                                            ^
In file included from /usr/include/c++/5/regex:61:0,
                 from moderncpp_json.cpp:3:
/usr/include/c++/5/bits/regex.h:2073:5: note: declared here
     regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,

How to avoid this error?

Thank,

Jean-Marc

@nlohmann
Copy link
Owner

nlohmann commented Sep 3, 2017

This seems to be this issue: https://stackoverflow.com/questions/33154890/simple-stdregex-search-code-wont-compile-with-apple-clang-std-c14 and related to a change in C++14

As a solution, query a reference with

if (std::regex_match(j["name"].get_ref<std::string&>(), m, r))

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Sep 3, 2017
@nlohmann
Copy link
Owner

nlohmann commented Sep 4, 2017

Does this work for you, @jm130794?

@jm130794
Copy link
Author

jm130794 commented Sep 6, 2017 via email

@nlohmann nlohmann closed this as completed Sep 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants