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

string replacement problem.

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


Posted by Maxhrk   USA  (76 posts)  Bio
Date Thu 20 Dec 2007 01:25 AM (UTC)

Amended on Thu 20 Dec 2007 01:28 AM (UTC) by Maxhrk

Message

local replacements = { 
   ['\\'] = "\\",
}

function toreplace(s)
	local result
	if type(s) == "string" then
	result = string.gsub (s, "%a+", 
				function (str) 
					return replacements [str] 
				end)
	return result
	else
		message.console("toreplace function: it is not a string.")
	end --if
end -- toreplace



when it display on my screen like this:

trigger:^You are tempered against fire damage.$


when it supposedly display like this:

trigger:^You are tempered against fire damage\.$


how to display '\' properly?

thanks!
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #1 on Thu 20 Dec 2007 01:47 AM (UTC)
Message
This is unnecessarily complicated for a start.

string.gsub will accept a table as the replacements argument, and substitute something found in the table for the replacement.

eg.


t = { cat = "dog", 
      mouse = "tiger" }

print ((string.gsub ("You see a cat and a mouse", "%a+", t))) --> You see a dog and a tiger



Your second problem is that you are searching for a backslash, and if found, replacing it with a backslash. What will that achieve?

Your third problem is that you are searching for "%a+" which is alphabetic characters. Thus it will never match on backslash anyway.

Something like this will achieve what I think you are trying to do:


test = "^You are tempered against fire damage.$"

test = string.gsub (test, "[^A-Za-z0-9 ]", "\\%1")

print (test)  --> \^You are tempered against fire damage\.\$


What I am searching for here is everything that is not A-Z or 0-9, or a space, and doing the replacement. Note how the tilde and $ got replaced too.


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


12,816 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.