Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. std::regex issue
Forum Updated to NodeBB v4.3 + New Features

std::regex issue

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 2 Posters 629 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on 28 Jul 2020, 14:30 last edited by Natural_Bugger
    #1

    hello, i trying to split a string witch contains a space into 2 parts..
    i have the following code.

            std::string str{""};
            getline(cin, str);
    
            std::vector<std::string> tokens;
            std::regex re(" ");
    
            //start/end points of tokens in str
            std::sregex_token_iterator
            begin(str.begin(), str.end(), re),
            end;
    
            std::copy(begin, end, std::back_inserter(tokens));
    
            string first_value = tokens[0];
            std::cout << "first_value: " << first_value << std::endl;
    
            string second_value = tokens[1];
            std::cout << "second_value" << second_value << std::endl;
    

    this is what the console outputs:

    sadd asdda // test string 1
    first_value:  
    terminate called after throwing an instance of 'std::logic_error'
      what():  basic_string::_M_construct null not valid
    

    according to debug mode:
    the first value = ""
    the second value = " \020" // 16 0x10

    what's going on?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Jul 2020, 17:47 last edited by
      #2

      Hi,

      Using a regular expression in this case seems overkill.

      What about the solution proposed in this Stack Overflow answer ?

      Note that QString has a split method that would suite your purpose pretty nicely.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply 28 Jul 2020, 19:03
      1
      • S SGaist
        28 Jul 2020, 17:47

        Hi,

        Using a regular expression in this case seems overkill.

        What about the solution proposed in this Stack Overflow answer ?

        Note that QString has a split method that would suite your purpose pretty nicely.

        N Offline
        N Offline
        Natural_Bugger
        wrote on 28 Jul 2020, 19:03 last edited by
        #3

        @SGaist
        hi, thnx for your reply,

        afterwards, i found this one.
        https://stackoverflow.com/questions/5844657/splitting-a-c-string-into-two-parts

                size_t found = str.find(" ");
                if(found != string::npos) {
                    string temp1 = (str.substr(0,found));
                    string temp2 = (str.substr(found+1, string::npos));
                }
        

        it did the job.

        i can't use the QT parts for these assignments.

        1 Reply Last reply
        1

        1/3

        28 Jul 2020, 14:30

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved