Nick Gammon's Regexp library

I have been trying to use the Regexp library (on Arduino UNO) to handle what I thought was a simple task.
I'm trying to develop a RE (regular expression) that can match the following text:

":c;" OR ":xx;"

The first text consists of exactly 3 character (the 'c' being any keyboard character).
The second text consists of exactly 4 characters (the 'xx' being exactly 2 HEX digits).

What I thought I could do was have a single RE that would match on either one of the possibilities;
-- either a single character between the ':' and ';' delimiters OR
-- 2 Hex digits between the delimiters

So far, I have not been successful. I'm now wondering if I'm going about the problem wrongly.

I've been reading Jeff Friedl's book "Mastering Regular Expression, 3rd Ed." to help.
Still struggling.
Ray

I haven’t used the library, but the standard syntax for your regular expression looks like this:

:(.|[A-Fa-f0-9]{2});

Use an online regex tester to check it.
It doesn’t work if the ‘keyboard character’ is an ‘;’ which is your deliminator.

6v6gt:
...which is your deliminator.

That word caught my eye. I was surprised to find it actually is in use and has a definition - deliminator.

6v6gt:
I haven't used the library, but the standard syntax for your regular expression looks like this:

:(.|[A-Fa-f0-9]{2});

Use an online regex tester to check it.
It doesn't work if the 'keyboard character' is an ';' which is your deliminator.

It does work even if the "keyboard character" is a semicolon. Try it out.

Thanks you for your replies.
Unfortunately, if you haven't used Nick's library, you're probably not aware that is doesn't use what "6v6gt" called "standard syntax".

I'm currently reading Jeff Friedl's "Mastering Regular Expressions, 3rd Ed." and Jeff quickly disabuses the reader of the notion there exists something you might wish to call "standard syntax" for regular expressions.

The expression provided i.e. ":(.|[A-Fa-f0-9]{2});" does NOT work with Nick's library.

It does work with regx101.com, but that tool can't help much with Nick's library.

Nick tries to be clear that he implemented his library heavily guided by the regular expression flavor (as Jeff Friedl calls the variations of regular expressions) found in the Lua programming language.

When I first embarked on this journey, I thought the regx101.com tool would help me, but I quickly found otherwise.

If in fact it can help, then I just added another problem to my already overcrowded problem set.

The first thing I noticed with the proposed RE, was the use of the OR metacharacter '|' (the verticle bar) to specify alternation.

That particular part of regular expression syntax (so common) is not available except in some very specific way (which I can't explain).

I also have trouble with quantifiers like "{}".

I'm hoping someone who has used Nick's library will reply.

In case you're wondering about whether I read the documentation -- I did, such as it is.

I'm also trying to figure out how to ask Nick himself about this problem -- still working on that one.

If any of you who replied can think of any alternatives without the use of the '|' character, please post it.

Thanks all;
Ray

You could create two separate RE matchers, one with

:(.);

and the other with

:([A-Fa-f0-9][A-Fa-f0-9]);

and test each pattern separately in your code.

Ok. Your rules are not very complex so there are alternatives to regular expressions.
You can use array of characters an check if the characters are in a range using their ascii values, or look at the String class.
If you are simply learning regular expressions, look a Perl for example.

christop;

Your solution works. There is a minor convenience in the use of "[%x]" to replace "[a-fA-F0-9]".

":(.);" works for exactly 1 character including ':' and ';'.

For ":([A-Fa-f0-9][A-Fa-f0-9]);" I used "[%x][%x]" and it works for exactly 2 hex digits.

Thanks for the help -- I wish I could see a way to do it with a single RE.

6v6gt;

If you are simply learning regular expressions, look a Perl for example.

I am trying to accomplish some useful work as well as learn RE(s).

It's a balancing act.

The use of Perl as a learning tool would probably not work very well as Nick's library is based on RE(s) used in Lua, which is quite a bit more restrictive than Perl.

I think that is why the regx101 tool isn't too useful -- it's heavily based on PCRE.

I already developed a way to deal with this issue using "strtok".

I also have another approach I'm considering but I'd like to learn as much as I can about RE(s).

That's why I bought Jeff Friedl's book.

Thanks to both of you for your help.
Ray