Programming Arduino with Regular Expressions
Download Code Here:
What is a Regular Expression?
A regular expression is a string that uses characters within the string to define a pattern and provides a function to test another string to find matches to that pattern. Probably the simplest regular expression is just a string with no special characters.You can use regular expressions with Arduino by adding a third party library to your sketch. You do that using Tools->Manage Libraries
Install Regexp by clicking the install button. This will download the required files and put them into the default library location defined by your IDE. You can find the location under File->Preferences if you changed the default, but the default on Windows is
C:\Users\<your_user_name>\Documents\Arduino\Libraries
C:\Users\<your_user_name>\Documents\Arduino\Libraries
If you look in your library location, you'll find a folder called Regexp that contains the required header file for this library and some other folders with the source code and examples that you can go through if you'd like.
Now, you need to add
Now, you need to add
#include "Regexp.h"
to your sketch.
Once you do that, you can start using Regular Expressions in your Arduino application.
In a nutshell, you'll need a MatchState object. This is the thing that does the work.
Then you set the MatchState.Target and call MatchState.Match(expression) passing in the expression to the Match() function. It returns 0 if no match is found, or 1 if it finds a match. Now, the MatchState.MatchStart will contain an index of the first match it found.
Then you set the MatchState.Target and call MatchState.Match(expression) passing in the expression to the Match() function. It returns 0 if no match is found, or 1 if it finds a match. Now, the MatchState.MatchStart will contain an index of the first match it found.
#include "Regexp.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
MatchState ms; //this is the object that does the work
char testString[] = "This is a test, telephone, no match"; // Matchstate expects char array
char regularExpression[] = "te[a-z]+"; //https://docs.microsoft.com/en-us/cpp/standard-library/regular-expressions-cpp?view=msvc-160
// this regular expression essentially says "find the text that starts with 'te', and then has any number of the remaining chars, but not spaces or numbers
ms.Target(testString); // the MatchState Target is the text that will be searched
int result = ms.Match(regularExpression); // you pass the regular expression to the match function.
// the result contains 1 if any matches are found, or 0 if not.
// ms.MatchStart contains the first index of anything that matches the pattern.
// ms.MatchLength contains the length of the match.
if(result>0){
// there is a match
for(int i=ms.MatchStart; i<ms.MatchStart+ms.MatchLength; i++){
Serial.print(testString[i]); // prints "test"
}
Serial.println();
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Now, if you need to match some text in a string, you can just use a regular expression.
Good Luck and Happy Coding!
~Tom
Good Luck and Happy Coding!
~Tom
Comments
Post a Comment