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 ➜ Regular Expressions

Regular Expressions

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


Posted by LupusFatalis   (154 posts)  Bio
Date Sat 24 Apr 2010 06:51 AM (UTC)

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
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 25 Apr 2010 01:33 AM (UTC)

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
Top

Posted by LupusFatalis   (154 posts)  Bio
Date Reply #2 on Sun 25 Apr 2010 03:04 PM (UTC)
Message
Works brilliantly, thanks Nick!
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.


11,392 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.