Regular expression (Regexp library)

Posted by Slackdrop on Sun 19 Mar 2017 03:30 AM — 4 posts, 21,544 views.

#0
Dear All,

This if my first post on this forum, so I apologize if it ended up in the wrong place.

I am trying to use the Regexp library that I got from this github: https://github.com/nickgammon/Regexp

I wish to implement the following regex expression from the standard <regex> library in c++11:

 ([R|V][-]?[[:digit:]]+([.][[:digit:]]+)?)?(V[-]?[[:digit:]]+([.][[:digit:]]+)?)? 


In essence, I want to detect the following possibilities:
1) R or V followed by [+/-decimal]
2) R followed by [+/-decimal] AND V followed by [+/-decimal]

Essentially, I am struggling to implement the AND part in the 2) case.

When I try to implement the above using Regexp, I can't do cover 1) and 2) using a single mask like I can using <regex> libraries.

I have to have two different masks.
I think the problem lies in fact that I do not know how to get Regexp to set the second submask as optional. Using <regex>, I would simply implement it as (submask1)(submask2)?.

Any ideas how to solve this?

Thanks a lot!

Anyways, Here is my solution which is incomplete. It only covers case 2)


^([R]([-]?)(%d+)([.]?)(%d*))(([V])([-]?)(%d+)([.]?)(%d*))$
Australia Forum Administrator #1
This should really be posted on the Arduino Forum.

Template:Arduino
Please post Arduino-related questions to the Arduino Forum or to StackExchange: Arduino. This saves splitting questions and answers between this forum and the Arduino ones.



However I see that you are expecting PCRE regular expression parsing, whereas the documentation makes it clear that this library implements Lua regular expressions (patterns).

Documentation for that is here:

Lua 5.2 Reference Manual - 6.4.1 – Patterns

Also here:

http://www.gammon.com.au/scripts/doc.php?lua=string.find

The Lua library does not allow for choices (eg. A|B) and also does not allow for optional groups (eg. (foo)? ).
#2
Thank you for your reply. I wasn't aware of the difference between PCRE and Lua parsing.

Regarding the [A|B] choices, what exactly do you mean? Because [A|B] works using your library. I have tested it quite exhaustively.

Kind regards!
Australia Forum Administrator #3
Ah, I see. You said:

Quote:

In essence, I want to detect the following possibilities:
1) R or V followed by [+/-decimal]


And using the Lua library you can't "or" things with a "|" symbol. For example, this won't work:


(cat)|(dog)


When you have this:


[R|V]


You have a set which matches a single character. That character can be anything inside the brackets, so it will match "R" or "|" or "V". The "|" is not an "or" character here, it is just another thing you are matching. So in your case you want:



[RV]


That matches R or V.