Reply to this subject
Start a new subject
 
Refresh page
| Posted by |
LupusFatalis
(154 posts) 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 | top |
|
| Posted by |
Nick Gammon
Australia (18,770 posts) bio
Forum Administrator |
| Date |
Reply #1 on 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 | 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,132 views.
Reply to this subject
Start a new subject
 
Refresh page
top
Comments to:
Gammon Software support
Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )