[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]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  string replacement problem.
Home  |  Users  |  Search  |  FAQ
Register forum user name
Forgotten password?

string replacement problem.

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

Posted by Maxhrk   USA  (76 posts)  [Biography] bio
Date Thu 20 Dec 2007 01:25 AM (UTC)

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


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

function toreplace(s)
	local result
	if type(s) == "string" then
	result = string.gsub (s, "%a+", 
				function (str) 
					return replacements [str] 
	return result
		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?

[Go to top] top

Posted by Nick Gammon   Australia  (21,607 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Thu 20 Dec 2007 01:47 AM (UTC)
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.


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


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.


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]