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.
Entire forum
➜ MUSHclient
➜ Lua
➜ Help with re:gmatch
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Zim
(22 posts) Bio
|
Date
| Mon 26 Jan 2015 01:16 AM (UTC) |
Message
| I'd like to take a block of text and create a table containing re matches. But I'm unsure of the correct way to do that. Here's a silly example to demonstrate what I'm trying to do.
text = "I like to eat apples for lunch. I like to eat pears for dinner. I don't like pizza. I like to eat veggies for fun!"
re = rex.new("I like to eat (.*?) for")
I'd like to end up with a table like:
1 apples
2 pears
3 veggies
How would I use re:gmatch to accomplish this? | Top |
|
Posted by
| Nick Gammon
Australia (23,121 posts) Bio
Forum Administrator |
Date
| Reply #1 on Mon 26 Jan 2015 02:30 AM (UTC) Amended on Mon 26 Jan 2015 02:31 AM (UTC) by Nick Gammon
|
Message
| It's easy with Lua regular expressions:
food = string.match ("I like to eat apples, pears, veggies for dinner",
"^I like to eat (.*) for")
t = { }
if food then
print (food)
for word in string.gmatch (food, "%a+") do
table.insert (t, word)
end -- for
else
print ("No match")
end -- if
require "tprint"
tprint (t)
Output:
apples, pears, veggies
1="apples"
2="pears"
3="veggies"
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Zim
(22 posts) Bio
|
Date
| Reply #2 on Mon 26 Jan 2015 02:56 AM (UTC) |
Message
| I appreciate the reply, but I need to know how to implement the new.rex because my actually code will require expressions that can't be done with basic lua matching. | Top |
|
Posted by
| Zim
(22 posts) Bio
|
Date
| Reply #3 on Mon 26 Jan 2015 03:13 AM (UTC) Amended on Mon 26 Jan 2015 03:16 AM (UTC) by Zim
|
Message
|
text = "I like to eat apples for lunch. I like to eat pears for dinner. I don't like pizza. I like to eat veggies for fun!"
re_food = rex.new("I like to eat (.*?) for")
my_matches = {}
re_food:gmatch(text, function (m, t)
for k, v in pairs(t) do
table.insert(my_matches, v)
end
end)
require "tprint"
tprint(my_matches)
I found that this works! But I still suspect there might be simpler syntax. | Top |
|
Posted by
| Nick Gammon
Australia (23,121 posts) Bio
Forum Administrator |
Date
| Reply #4 on Mon 26 Jan 2015 06:46 AM (UTC) |
Message
| Looks pretty simple to me. |
- 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,983 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top