mirror of
https://github.com/lsdlsd88/JC4827W543.git
synced 2026-05-02 23:29:29 +01:00
initial commit
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
#include <Regexp.h>
|
||||
|
||||
// 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
|
||||
|
||||
Serial.print ("Matched: ");
|
||||
Serial.write ((byte *) match, length);
|
||||
Serial.println ();
|
||||
|
||||
for (byte i = 0; i < ms.level; i++)
|
||||
{
|
||||
Serial.print ("Capture ");
|
||||
Serial.print (i, DEC);
|
||||
Serial.print (" = ");
|
||||
ms.GetCapture (cap, i);
|
||||
Serial.println (cap);
|
||||
} // end of for each capture
|
||||
|
||||
} // end of match_callback
|
||||
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin (115200);
|
||||
Serial.println ();
|
||||
unsigned long count;
|
||||
|
||||
// what we are searching (the target)
|
||||
char buf [100] = "The quick brown fox jumps over the lazy wolf";
|
||||
|
||||
// match state object
|
||||
MatchState ms (buf);
|
||||
|
||||
// original buffer
|
||||
Serial.println (buf);
|
||||
|
||||
// search for three letters followed by a space (two captures)
|
||||
count = ms.GlobalMatch ("(%a+)( )", match_callback);
|
||||
|
||||
// show results
|
||||
Serial.print ("Found ");
|
||||
Serial.print (count); // 8 in this case
|
||||
Serial.println (" matches.");
|
||||
|
||||
|
||||
} // end of setup
|
||||
|
||||
void loop () {}
|
||||
@@ -0,0 +1,61 @@
|
||||
#include <Regexp.h>
|
||||
|
||||
// called for every match
|
||||
void replace_callback (const char * match, // what we found
|
||||
const unsigned int length, // how long it was
|
||||
const char * & replacement, // put replacement here
|
||||
unsigned int & replacement_length, // put replacement length here
|
||||
const MatchState & ms) // for looking up captures
|
||||
{
|
||||
|
||||
// show matching text
|
||||
Serial.print("Match = ");
|
||||
Serial.write((byte *) match, length);
|
||||
Serial.println ();
|
||||
|
||||
replacement = "Nick";
|
||||
replacement_length = 4;
|
||||
} // end of replace_callback
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin (115200);
|
||||
Serial.println ();
|
||||
unsigned long count;
|
||||
|
||||
// what we are searching (the target)
|
||||
char buf [100] = "The quick brown fox jumps over the lazy wolf";
|
||||
|
||||
// match state object
|
||||
MatchState ms (buf);
|
||||
|
||||
// original buffer
|
||||
Serial.println (buf);
|
||||
|
||||
// search for three letters
|
||||
count = ms.GlobalReplace ("%a+", replace_callback);
|
||||
|
||||
// show results
|
||||
Serial.print ("Converted string: ");
|
||||
Serial.println (buf);
|
||||
Serial.print ("Found ");
|
||||
Serial.print (count); // 9 in this case
|
||||
Serial.println (" matches.");
|
||||
|
||||
// copy in new target
|
||||
strcpy (buf, "But does it get goat's blood out?");
|
||||
ms.Target (buf); // recompute length
|
||||
|
||||
// replace vowels with *
|
||||
count = ms.GlobalReplace ("[aeiou]", "*");
|
||||
|
||||
// show results
|
||||
Serial.print ("Converted string: ");
|
||||
Serial.println (buf);
|
||||
Serial.print ("Found ");
|
||||
Serial.print (count); // 13 in this case
|
||||
Serial.println (" matches.");
|
||||
|
||||
} // end of setup
|
||||
|
||||
void loop () {}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <Regexp.h>
|
||||
|
||||
// called for every match
|
||||
void replace_callback (const char * match, // what we found
|
||||
const unsigned int length, // how long it was
|
||||
const char * & replacement, // put replacement here
|
||||
unsigned int & replacement_length, // put replacement length here
|
||||
const MatchState & ms) // for looking up captures
|
||||
{
|
||||
static byte c; // for holding replacement byte, must be static
|
||||
|
||||
char hexdigits [3]; // to hold hex string
|
||||
|
||||
// get first capture
|
||||
ms.GetCapture (hexdigits, 0);
|
||||
// convert from hex to printable
|
||||
c = strtol (hexdigits, NULL, 16);
|
||||
|
||||
// set as replacement
|
||||
replacement = (char *) &c;
|
||||
replacement_length = 1;
|
||||
} // end of replace_callback
|
||||
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin (115200);
|
||||
|
||||
// what we are searching
|
||||
char buf [100] = "%7B%22John+Doe%22%7D";
|
||||
|
||||
// for matching regular expressions
|
||||
MatchState ms (buf);
|
||||
|
||||
// easy part, replace + by space
|
||||
ms.GlobalReplace ("%+", " ");
|
||||
|
||||
// replace %xx (eg. %22) by what the hex code represents
|
||||
ms.GlobalReplace ("%%(%x%x)", replace_callback);
|
||||
|
||||
Serial.println (buf);
|
||||
|
||||
} // end of setup
|
||||
|
||||
void loop () {}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <Regexp.h>
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin (115200);
|
||||
|
||||
// match state object
|
||||
MatchState ms;
|
||||
|
||||
// what we are searching (the target)
|
||||
char buf [100] = "The quick brown fox jumps over the lazy wolf";
|
||||
ms.Target (buf); // set its address
|
||||
Serial.println (buf);
|
||||
|
||||
char result = ms.Match ("f.x");
|
||||
|
||||
if (result > 0)
|
||||
{
|
||||
Serial.print ("Found match at: ");
|
||||
Serial.println (ms.MatchStart); // 16 in this case
|
||||
Serial.print ("Match length: ");
|
||||
Serial.println (ms.MatchLength); // 3 in this case
|
||||
}
|
||||
else
|
||||
Serial.println ("No match.");
|
||||
|
||||
} // end of setup
|
||||
|
||||
void loop () {}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <Regexp.h>
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin (115200);
|
||||
|
||||
// match state object
|
||||
MatchState ms;
|
||||
|
||||
// what we are searching (the target)
|
||||
char buf [100] = "The quick brown fox jumps over the lazy wolf";
|
||||
ms.Target (buf); // set its address
|
||||
|
||||
unsigned int count = ms.MatchCount ("[aeiou]");
|
||||
|
||||
Serial.println (buf);
|
||||
Serial.print ("Found ");
|
||||
Serial.print (count); // 11 in this case
|
||||
Serial.println (" matches.");
|
||||
|
||||
} // end of setup
|
||||
|
||||
void loop () {}
|
||||
Reference in New Issue
Block a user