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

Gammon Software Solutions forum

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

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Regular Expressions

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: Regular Expressions
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please).
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

Posted by LupusFatalis   (154 posts)  [Biography] bio
Date Sun 25 Apr 2010 03:04 PM (UTC)  quote  ]
Message
Works brilliantly, thanks Nick!
[Go to top] top

Posted by Nick Gammon   Australia  (18,801 posts)  [Biography] bio   Forum Administrator
Date Sun 25 Apr 2010 01:33 AM (UTC)  quote  ]

Amended on Sun 25 Apr 2010 01:34 AM (UTC) by Nick Gammon

Message
This seems to work OK:


function process_name (room_name)

  -- long regular expressions [ordered]
  reName = {"^(A|An) (somewhat |very |very very |extremely )?(small |large )?cave$",
      "^(A|An) (somewhat |very |very very |extremely )?(small |large )?cavern$",
      "^(A|An) (somewhat |very |very very |extremely )?(small |large )?tunnel$"}

  -- short strings [ordered]
  sName = {"A cave",
      "A cavern",
      "A tunnel"}

  -- If a match, return corresponding short strings
  for i,item in ipairs(reName) do
    if rex.new (item):match (room_name) then
       return sName [i]
    end -- if
  end  -- for

  return roon_name

end -- function

-- tests

print (process_name ("A somewhat large cave"))   --> A cave
print (process_name ("A somewhat small cavern"))  --> A cavern
print (process_name ("An extremely large tunnel"))  --> A tunnel


- Nick Gammon

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

Posted by LupusFatalis   (154 posts)  [Biography] bio
Date Sat 24 Apr 2010 06:51 AM (UTC)  quote  ]

Amended on Sat 24 Apr 2010 07:15 AM (UTC) by LupusFatalis

Message
Ultimately what I'm trying to do here is match a string against a list of regular expressions and have a second list of strings to substitute.
function process_name (room_name)

	-- long regular expressions [ordered]
	reName = {"^(A|An) (somewhat |very |very very |extremely )?(small |large )?cave$",
			"^(A|An) (somewhat |very |very very |extremely )?(small |large )?cavern$",
			"^(A|An) (somewhat |very |very very |extremely )?(small |large )?tunnel$"}

	-- short strings [ordered]
	sName = {"A cave",
			"A cavern",
			"A tunnel"}

	--Convert matched long regular expressions to corresponding short strings
	for i,item in ipairs(reName) do
		room_name = rex.gsub(room_name, item, sName[i])
	end

	return roon_name

I've tried a bunch of things now, and I just can't seem to get it to work properly.

Though, this seems to indicate I'm doing it correctly: http://math2.org/luasearch/rex.html#rex_gsub
[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,159 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

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

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]