Need help parsing..
-
@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 -
@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 scopeNOTE: 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 usrBest regards
-
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 -
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
-
@Sato Yes, because Serial.println requires String and not std::string...
Try
Serial.println(dev_str.c_str())
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 -
@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 -
-
@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 -
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