[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  Regular expression (Regexp library)
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Regular expression (Regexp library)

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Slackdrop   (2 posts)  [Biography] 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*))$
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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.

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)? ).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Slackdrop   (2 posts)  [Biography] 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!
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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:


(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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] 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.


1,301 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]