GUI-O Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Need help parsing..

    Scheduled Pinned Locked Moved
    Comments & Feedback
    3
    11
    626
    Loading More Posts
    • 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.
    • S
      Sato
      last edited by

      Hi,
      I am not a C++ expert, so maybe someone on the Forum can help how to solve the Parse of the following

      @init dev:OF21 Av. John XXI uuid:d3f1c986-16b3-4758-9724-51da22471f0c

      How can I store the following characters in a variable1
      OF21 Av. John XXI

      as well as the following in a variable2
      d3f1c986-16b3-4758-9724-51da22471f0c

      Thanks
      Best regards

      K 1 Reply Last reply Reply Quote 0
      • K
        kl3m3n @Sato
        last edited by kl3m3n

        @Sato Hi.

        One option using STL:

        #include <string>
        
        static const std::string dev_token{"dev:"};
        static const std::string uuid_token{"uuid:"};
            
        auto dev_s = str.find(dev_token);
        auto uuid_s = str.find(uuid_token);
            
        if(dev_s != std::string::npos 
           && uuid_s != std::string::npos) {
             dev_s += dev_token.length();
             const auto dev_str = str.substr(dev_s, uuid_s - dev_s - 1); // -1 removes the trailing whitespace
                
             uuid_s += uuid_token.length();
             const auto uuid_str = str.substr(uuid_s, 36); // uuid is always 36 characters
                
             // dev_str and uuid_str are the desired payloads
        }
        

        NOTE: If you enable sending usr, this will not work as the usr payload is inserted between dev and uuid...

        Best regards,
        kl3m3n

        S 1 Reply Last reply Reply Quote 0
        • S
          Sato @kl3m3n
          last edited by

          @kl3m3n
          Thank you kl3m3n,

          Unfortunately I not undestand the code, but it gives compile error

          The @init dev: OF21 Av. John XXI uuid:d3f1c986-16b3-4758-9724-51da22471f0c is in the msg variable

          I try
          auto dev_s = msg.find("dev:");
          auto uuid_s = msg.find("uuid:");
          compile error: const class String has no member named 'find'

          and that can't compile too
          auto dev_s = str.find("dev:");
          auto uuid_s = str.find("uuid:");
          compile error: 'str' was not declared in this scope

          NOTE: If you enable sending usr, this will not work as the usr payload is inserted between dev and uuid...
          I know, on startup don´t needed, later during the programm starting i get the usr

          Best regards

          K 1 Reply Last reply Reply Quote 0
          • K
            kl3m3n @Sato
            last edited by

            @Sato

            str is your msg 🙂

            You using Arduino right?

            Try:

            std::string str = std::string(msg.c_str());
            

            and then use my code...

            Although it seems unnecessary to convert between String and std::string... You can write the same code using Arduino String functions (using indexOf(), etc...):

            https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/

            Regards,
            kl3m3n

            S 1 Reply Last reply Reply Quote 0
            • S
              Sato @kl3m3n
              last edited by

              @kl3m3n

              Yes, I use Arduino
              (using indexOf(), etc...):
              That's how I was doing it, but I wasn't sure that the uuid length was fixed.

              Your code compiles, but gives an error if you want to see the contents of dev_str and uuid_str in the serial monitor
              no matching function for call to 'HardwareSerial::println(const std::__cxx11::basic_string<char>&)'

              Best regards

              K 1 Reply Last reply Reply Quote 0
              • K
                kl3m3n @Sato
                last edited by

                @Sato Yes, because Serial.println requires String and not std::string...

                Try

                Serial.println(dev_str.c_str())
                

                kl3m3n

                S 1 Reply Last reply Reply Quote 0
                • S
                  Sato @kl3m3n
                  last edited by

                  @kl3m3n,

                  I still don't understand your code, but I will try to learn. Compared to (using indexOf(), etc...) your code is more elegant, I will use it if you don't mind.

                  Your knowledge is impressive, you are still the best

                  Thanks
                  Regards

                  K 1 Reply Last reply Reply Quote 0
                  • K
                    kl3m3n @Sato
                    last edited by

                    @Sato said in Need help parsing..:

                    I will use it if you don't mind.

                    Of course you can use it, I don't mind.

                    Best regards,
                    kl3m3n

                    S 1 Reply Last reply Reply Quote 0
                    • S
                      Sato @kl3m3n
                      last edited by

                      @kl3m3n,

                      OK, Thank you

                      Regards

                      1 Reply Last reply Reply Quote 0
                      • G
                        gammda
                        last edited by gammda

                        @Sato
                        If you use Bascom, this function (GetToken) is the best for parsing text.
                        [https://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=2543&highlight=gettoken

                        S 1 Reply Last reply Reply Quote 0
                        • S
                          Sato @gammda
                          last edited by

                          Hi @gammda,

                          Thank you. For this project, this time i not use Bascom, i use ESP32 and Arduino.
                          The code form Kl3m3n works fine.

                          Best regards

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post