Find substring in array of chars, parse it and save to new char array

Hello, I have array of chars and I need parse this array. Array start and ends with unimportant characters.
I need find "btn1=1" or "btn1=0" and save value to boolean btn1 and so on... Sha1 is always 40 chars long.
At the end I need copy all important chars (bold text) to my new array allParams.
Input is always same. Only number is different (btn1=1"/"btn1=0") and sha hash is different.
I try:

#include <string.h>
void setup() {
Serial.begin(9600);

char input[] = {"some unimportant \n databtn1=1&btn2=0&sha1=a5b5da1c91aa2ea0c7e630db6c8e3175058e907f some\t unimportant \ndata"};
boolean btn1;
boolean btn2;
char sha1[40];
char allParams[100];

char tmp[6];
if ( strncpy(tmp, strstr(input,"btn1=1"), 6 ) )
btn1 = true;
if ( strncpy(tmp, strstr(input,"btn1=0"), 6 ) )
btn1 = false;
Serial.println(btn1);
}
void loop() {}

but it doesn't work. Can you help me, please?

I write working code for parse btn1 and btn2:

#include <string.h>
void setup() {
Serial.begin(9600);

char input[] = {"some unimportant \n data btn1=0&btn2=1&sha1=a5b5da1c91aa2ea0c7e630db6c8e3175058e907f some \nunimportant \ndata"};
boolean btn1;
boolean btn2;
char sha1[40];
char allParams[100];
memset(allParams, 0, 100);
strcat(allParams, "?");

if ( strstr(input, "btn1=0") ) {
strcat(allParams, "btn1=0&");
btn1 = false;
}
if ( strstr(input, "btn1=1") ) {
strcat(allParams, "btn1=1&");
btn1 = true;
}

if ( strstr(input, "btn2=0") ) {
strcat(allParams, "btn2=0&");
btn2 = false;
}
if ( strstr(input, "btn2=1") ) {
strcat(allParams, "btn2=1&");
btn2 = true;
}

Serial.println(btn1);
Serial.println(btn2);
Serial.println(allParams);

}
void loop() {}

Output:

0
1
?btn1=0&btn2=1&

but I still can't parse sha1 hash. How I can use regular expressions in Arduino?
Is regex (sha1=)\w{40} good?

martin159:
... but it doesn't work. Can you help me, please?

Here's what I think you expected: this sketch would print the character, "1".
Here's what I think you got: the sketch prints "0".
A "0" seems to indicate that the text "btn1=1" wasn't found in the string, when in fact that text is in the string.

Here's what I think is wrong: This code -

  if (   strncpy(tmp, strstr(input,"btn1=1"), 6 )  )

tests the return value of strncpy(). That return value will always be tmp, a pointer a character array: strncpy() returns it's destination argument. That won't be the null pointer, so the test will always evaluate as true.

Here's what I think you want to do:

  if (   strstr(input,"btn1=1")  ) {

If strstr() finds the text in the input string, it will return a pointer to the first character of that text; that will evaluate as true. If it doesn't, it will return the null pointer, which will evaluate as false.

If you want to store the string in the array tmp[], you'll need to execute strncpy() to capture it. You'll also need to make it's size 7 instead of 6, to make room for the terminating null character, and you'll have to add that null character yourself, because strncpy doesn't do it. There's not much point in keeping it, though - if it contains anything, you already know exactly what it contains.

For your conveninece, here's a link to instructions about posting to this forum: Read this before posting a programming question ... - Programming Questions - Arduino Forum. Relevant issues are covered in item 6.

martin159:
... but I still can't parse sha1 hash.

I don't see anything in the code that looks for "sha1".

How I can use regular expressions in Arduino?

I don't know of a way to use regular expressions on the Arduino. Someone may have written a limited-function library; I'll leave that search to you and Google.

Is regex (sha1=)\w{40} good?

How did it work when you tried it?

I think that your best bet would be to look for "sha1=", just like you look for button identifiers, and then capture those characters and the next 40 characters. You can trust that the characters are correct, or you can test the characters to make sure that they're valid hex.

OK, I do it without regex.
I find copy function.

#include <string.h>
#include <string>
void setup() {
 Serial.begin(9600);
 char input[] = {"some unimportant \n data btn1=0&btn2=1&sha1=a5b5da1c91aa2ea0c7e630db6c8e3175058e907f some \nunimportant \ndata"};
 boolean btn1;
 boolean btn2;  
 char sha1[40];
 char allParams[100];
 memset(allParams, 0, 100);
 strcat(allParams, "?");

 if ( strstr(input, "btn1=0") ) {
   strcat(allParams, "btn1=0&");
   btn1 = false;
 } 
 if ( strstr(input, "btn1=1") ) {
   strcat(allParams, "btn1=1&");
   btn1 = true;
 } 
 
 if ( strstr(input, "btn2=0") ) {
   strcat(allParams, "btn2=0&");
   btn2 = false;
 } 
 if ( strstr(input, "btn2=1") ) {
   strcat(allParams, "btn2=1&");
   btn2 = true;
 }
 
               input.copy(sha1, 40, (strstr(input, "sha1=")+5)  ); 
 
  Serial.println(btn1);
  Serial.println(btn2);
  Serial.println(sha1);
  Serial.println(allParams);
}
void loop() {}

I get error:

TextFind.ino: In function ‘void setup()’:
TextFind:34: error: request for member ‘copy’ in ‘input’, which is of non-class type ‘char [108]’

Now it works fine. Working code:

#include <string.h>
void setup() {
Serial.begin(9600);

char input[] = {"some unimportant \n data btn1=0&btn2=1&sha1=a5b5da1c91aa2ea0c7e630db6c8e3175058e907f some \nunimportant \ndata"};
char sha1[41];
memset(sha1, 0, 41);
memcpy(sha1, (strstr(input, "sha1=")+5), (sizeof(char)*40) );
Serial.println(sha1);

}
void loop() {}