#include <Regexp.h>
#define ArrLen(x) (sizeof(x)/sizeof(x[0]))
unsigned int RegxCheck(const char* buf, const char* rgx = "^(%d%d):(%d%d)") {
unsigned int count = 0;
MatchState ms;
ms.Target((char*)buf);
return (ms.MatchCount(rgx));
}
void setup () {
Serial1.begin (115200);
Serial1.println();
const char* rgx = "^(%d%d):(%d%d)";
const char* buf = "18:00 hello";
int match = RegxCheck(buf, rgx);
Serial1.print("buf: "); Serial1.println(buf);
Serial1.print("match: "); Serial1.println(match);
}
void loop () {}
/////////////////////////////////////////////////////////////////////////////////////
/*
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;
idx = (idx > 0) ? idx-1 : 0;
}
return idx;
}
bool strHeader(char* buf) {
const char* proto = "00:00";
char stor, tmp[strlen(buf)+8] = {'\0'};
Serial1.print("buf: "); Serial1.println(buf);
int err = validStr(buf, strlen(proto));
int delim = (strlen(proto)-err);
if (!err)
return false;
strcpy(tmp, buf);
strcpy(buf, "");
stor = tmp[err]; Serial1.print("stored: '"); Serial1.print(stor); Serial1.println("'");
tmp[delim] = '\0';
strcat(buf, tmp);
const char* rem = (proto + delim);
Serial1.print(tmp);
Serial1.println(rem);
//Serial1.println(proto + (strlen(proto)-err));
return true;
}
void setup () {
Serial1.begin (115200);
Serial1.println ();
MatchState ms;
ms.Target(buf);
const char* rgx = "^(%d%d):(%d%d)";
unsigned int count = ms.MatchCount(rgx);
Serial1.print("buf: "); Serial1.println(buf);
Serial1.print("count: "); Serial1.println(count);
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 () {}
*/