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

Need help parsing..

Scheduled Pinned Locked Moved
Comments & Feedback
3
11
661
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 17 Feb 2023, 16:59

    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 17 Feb 2023, 18:36 Reply Quote 0
    • K
      kl3m3n @Sato
      last edited by kl3m3n 17 Feb 2023, 18:36

      @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 17 Feb 2023, 19:08 Reply Quote 0
      • S
        Sato @kl3m3n
        last edited by 17 Feb 2023, 19:08

        @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 17 Feb 2023, 19:14 Reply Quote 0
        • K
          kl3m3n @Sato
          last edited by 17 Feb 2023, 19:14

          @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 17 Feb 2023, 19:32 Reply Quote 0
          • S
            Sato @kl3m3n
            last edited by 17 Feb 2023, 19:32

            @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 17 Feb 2023, 19:51 Reply Quote 0
            • K
              kl3m3n @Sato
              last edited by 17 Feb 2023, 19:51

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

              Try

              Serial.println(dev_str.c_str())
              

              kl3m3n

              S 1 Reply Last reply 17 Feb 2023, 20:22 Reply Quote 0
              • S
                Sato @kl3m3n
                last edited by 17 Feb 2023, 20:22

                @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 18 Feb 2023, 10:58 Reply Quote 0
                • K
                  kl3m3n @Sato
                  last edited by 18 Feb 2023, 10:58

                  @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 18 Feb 2023, 11:08 Reply Quote 0
                  • S
                    Sato @kl3m3n
                    last edited by 18 Feb 2023, 11:08

                    @kl3m3n,

                    OK, Thank you

                    Regards

                    1 Reply Last reply Reply Quote 0
                    • G
                      gammda
                      last edited by gammda 24 Feb 2023, 21:39

                      @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 27 Feb 2023, 13:06 Reply Quote 0
                      • S
                        Sato @gammda
                        last edited by 27 Feb 2023, 13:06

                        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
                        4 out of 11
                        • First post
                          4/11
                          Last post