Register forum user name Search FAQ

Gammon Forum

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 ➜ MUSHclient ➜ Lua ➜ string finding a whole word only?

string finding a whole word only?

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


Posted by Rivius   (99 posts)  Bio
Date Fri 06 May 2011 05:56 PM (UTC)
Message
I was wondering what the regex syntax would be for finding a word would look like.

For example:

string.find("banana bananapeel", "banana")

returns true on both banana and bananapeel. However, I want it to strictly match just the word "banana". How would I do this?
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #1 on Fri 06 May 2011 06:27 PM (UTC)
Message
Lua's built-in pattern matching library doesn't really have a word-border match, but PCRE does:
local re = rex.new("\bbanana\b")
-- re can be created once and used many times
local s, e, matches = re:match("banana bananapeel")


http://www.gammon.com.au/forum/?id=4905

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Fiendish   USA  (2,535 posts)  Bio   Global Moderator
Date Reply #2 on Fri 06 May 2011 08:15 PM (UTC)
Message
Lua can do anything!


my_string = "bananaphone banana"
find_me = "banana"
start,finish = string.find(string.gsub(my_string,"(.*)"," %1 "), "[^%a]"..find_me.."[^%a]")
finish = finish-2


:D

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #3 on Fri 06 May 2011 10:26 PM (UTC)
Message
Also see:

Template:post=6034 Please see the forum thread: http://gammon.com.au/forum/?id=6034.


Scroll down to near the bottom about the "frontier pattern". That does exactly what you want, because it detects the transition between letters and non-letters.

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


19,591 views.

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

Go to topic:           Search the forum


[Go to top] top

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