#include <Regexp.h>
#define ArrLen(x) (sizeof(x)/sizeof(x[0]))
const char* items[] = {
"18:00: aftensmad",
"09:00: bus",
"20:00: putte",
"14:00: hente 1430",
"08:00: staa op",
"19:00: bad",
};
// called for each match
void match_callback (const char * match, // matching string (not null-terminated)
const unsigned int length, // length of matching string
const MatchState & ms) { // MatchState in use (to get captures)
char cap [10]; // must be large enough to hold captures
Serial1.print ("Matched: '");
Serial1.write ((byte *) match, length);
Serial1.write('\x27');
Serial1.println ();
for (byte i = 0; i < ms.level; i++) {
Serial1.print ("Capture ");
Serial1.print (i, DEC);
Serial1.print (" = ");
ms.GetCapture (cap, i);
Serial1.println (cap);
} // end of for each capture
} // end of match_callback
void rgxCount(char* buf = nullptr, const char* rgx = "[0-9]"){
// match state object
MatchState ms;
// what we are searching (the target)
ms.Target (buf); // set its address
unsigned int count = ms.MatchCount(rgx);
Serial1.print("buf: "); Serial1.println (buf);
Serial1.print ("Found "); Serial1.print (count); Serial1.println (" matches.");
} // end of setup
/*
int validStr(const char* buf, int idx = 5) {
int n = idx;
if (strlen(buf) < idx)
return idx;
for (int i = 0; i<n; i++) {
bool valid = (bool)((i != 2) ? (isDigit(buf[i])) : (buf[i] == ':'));
if (!valid) break;
Serial1.print(buf[i]);
idx = (idx > 0) ? idx-1 : 0;
}
Serial1.print("\nidx: ");
Serial1.println(idx);
return idx;
}
*/
int validStr(const char* buf, int idx = 5) {
int n = idx;
if (strlen(buf) < idx)
return idx;
for (int i = 0; i<n; i++) {
bool valid = (bool)((i != 2) ? (isDigit(buf[i])) : (buf[i] == ':'));
if (!valid) break;
idx = (idx > 0) ? idx-1 : 0;
}
return idx;
}
bool strHeader(char* buf) {
const char* proto = "00:00";
int err = 0;
Serial1.print("buf: "); Serial1.println(buf);
err = validStr(buf, strlen(proto));
if (!err) return false;
Serial1.println(proto + (strlen(proto)-idx));
return true;
}
void setup () {
Serial1.begin (115200);
Serial1.println ();
char str[] = "18:00 - aftensmad";
strHeader(str);
const char *s = "18 - aftensmad";
strHeader((char*)s);
//validStr(str, 5);
//validStr("18 - aftensmad", 5);
/*
char buf[] = "18:00 - aftensmad";
int idx = 0;
bool valid = true;
if (strlen(buf) >= 5) {
for (int i = 0; i<5; i++) {
valid = (bool)((i != 2) ? (isDigit(buf[i])) : (buf[i] == ':'));
Serial1.print(i);
Serial1.print(": ");
Serial1.print(buf[i]);
Serial1.print(" -> ");
Serial1.println((valid) ? "valid" : "invalid");
}
}
else
Serial1.println("strlen() < 5!");
while (1);
*/
/*
char buf[] = "18:00 - aftensmad";
rgxCount(buf, "^(%d%d)[:]*");
while (1);
unsigned long count;
// match state object
MatchState ms (buf);
// original buffer
Serial1.println(buf);
// search for three letters followed by a space (two captures)
//count = ms.GlobalMatch ("(%a+)( )", match_callback);
count = ms.GlobalMatch("^(%d%d):(%d%d)", match_callback);
// show results
Serial1.print ("Found ");
Serial1.print (count); // 8 in this case
Serial1.println (" matches.");
*/
}
void loop () {}