Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ Programming
➜ General
➜ Regular expression (Regexp library)
Regular expression (Regexp library)
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Slackdrop
(2 posts) Bio
|
Date
| Sun 19 Mar 2017 03:30 AM (UTC) |
Message
| 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*))$
| Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #1 on Sun 19 Mar 2017 06:46 AM (UTC) |
Message
| This should really be posted on the Arduino Forum.
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)? ). |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Slackdrop
(2 posts) Bio
|
Date
| Reply #2 on Sun 19 Mar 2017 04:48 PM (UTC) |
Message
| 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! | Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #3 on Mon 20 Mar 2017 06:33 AM (UTC) |
Message
| 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:
When you have this:
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:
That matches R or V. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
16,705 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top