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 ➜ Silly question, with what I'm sure has a simple answer

Silly question, with what I'm sure has a simple answer

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


Posted by Rivius   (99 posts)  Bio
Date Sat 11 Jun 2011 08:22 AM (UTC)
Message
I'm trying to make a function to do a search, however, I encounter some troubles when I use hyphenated words.

For example string.find("hyphenated-word", "hyphenated-word") will give no result for me.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Sat 11 Jun 2011 08:48 AM (UTC)
Message
Hyphen is one of the regexp "magic" symbols. Try putting a % in front of it.


print (string.find("hyphenated-word", "hyphenated%-word") ) --> 1 15


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Rivius   (99 posts)  Bio
Date Reply #2 on Sat 11 Jun 2011 05:10 PM (UTC)
Message
Hrm, well I've tried dealing with the string by doing this:

local what = string.gsub("%1", "-", "%-")

and this

local what = string.gsub("%1", "%-", "%-")

But it doesn't seem to help.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #3 on Sat 11 Jun 2011 10:56 PM (UTC)
Message
What are you trying to do here? Replace a hyphen by a hyphen?

You need the % in the regular expression, eg.


what = string.gsub("hyphenated-word", "%-", "(hello)")
print (what)  --> hyphenated(hello)word


However I should point out that if you are doing this in "send to script" that the % symbol has its own meaning (eg. %1, %2, and so on), so if using regular expressions you have to double them.

Eg (in send-to-script box):


what = string.gsub("hyphenated-word", "%%-", "(hello)")
print (what)  --> hyphenated(hello)word

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Rivius   (99 posts)  Bio
Date Reply #4 on Sun 12 Jun 2011 05:23 AM (UTC)
Message
Ah yes. This is in a script file and is a search function to find room names in my map databased. Here's an excerpt of the code


if string.find(string.lower(map_table[a].name), string.lower(string.gsub(name, "%-", "%-")))


I was just trying to make it so that typing "cloud-formation" would still give a match. At the moment "cloud-" and "formation" do, but the other doesn't because of the regex problem I presume.

I'm sorry if I'm doing this entirely improperly :P
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #5 on Sun 12 Jun 2011 07:07 AM (UTC)
Message
If it's in a script file, then the syntax is OK but I don't know what you are trying to achieve. See this:


print ((string.gsub("hyphenated-word", "%-", "%-"))) --> hyphenated-word


You are replacing a hyphen with a hyphen. Why?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Rivius   (99 posts)  Bio
Date Reply #6 on Sun 12 Jun 2011 07:32 AM (UTC)
Message
Well, I have an alias where you enter a query and it searches the map table and returns matches. I encountered a problem where it wouldn't return matches if I typed a hyphenated word.
Top

Posted by Rivius   (99 posts)  Bio
Date Reply #7 on Mon 13 Jun 2011 08:39 PM (UTC)
Message
I've figured out the problem. Strangely enough, I had to double escape the second hyphen.

string.lower(string.gsub(name, "%-", "%%-"))

But now it works. Thanks.
Top

Posted by Larkin   (278 posts)  Bio
Date Reply #8 on Wed 15 Jun 2011 02:51 PM (UTC)
Message
You didn't double escape the hyphen, technically. You had to escape the percent sign in order to replace your hyphen with percent hyphen. In fact, %%%- would probably be more correct, escaping both symbols.
Top

Posted by Rivius   (99 posts)  Bio
Date Reply #9 on Sat 25 Jun 2011 06:54 PM (UTC)
Message
Ah. That makes sense. I didn't even think of it that way.
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.


30,658 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.