so i am getting lots of things in a string using string.match but it is only returning one of the things i want how can i make it return more than one thing?
btw the amount of things im finding is random
so i am getting lots of things in a string using string.match but it is only returning one of the things i want how can i make it return more than one thing?
btw the amount of things im finding is random
Use capturing groups. For example:
("ab"):match("(a)(b)") --> a, b
returns "a"
and "b"
.
btw the amount of things im finding is random becuase using httpservice
What data are you getting from HttpService and what do you need to capture out of it?
Im trying to get synonyms from https://www.thesaurus.com to use to make a feature where you can use different words in chat
this is what ive got
local HttpService = game:GetService("HttpService")
local UniformResourceLocator = "https://www.thesaurus.com/browse/synonym"
local GetAsync = HttpService:GetAsync(UniformResourceLocator)
local String = string.match(GetAsync, "\"term\":%b\"\"")
Ah I see. So when it comes to having code talk with websites, typically you want to go through an “API” (application programming interface) instead of just the front-end website. Simply going through the front-end website requires “scraping” for content, which is typically considered a bad practice.
Nowadays, APIs usually send back JSON data, which can be transformed into a Lua table using HttpService:JSONDecode()
.
I’m not sure that thesaurus.com has a public API though.
I doubt it does and i dont like using apis i managed to do it with dictionary.com because I only had to use 1 string.
local HttpService = game:GetService("HttpService")
local Module = {}
function Module:Define(String)
local UniformResourceLocator = "https://www.dictionary.com/browse/" .. String
local GetAsync = HttpService:GetAsync(UniformResourceLocator)
local String2 = string.match(GetAsync, "content=%b\"\"")
String2 = string.gsub(String2, "content=", "")
String2 = string.gsub(String2, " See more.", "")
return String2
end
local Module:Synonym = function(String)
end
return Module
Maybe using string.split will work?
thesaurus.com is clearly randomizing their class names within the HTML elements. They’re doing this specifically to keep people from scraping for content. I’d say it’s best to stay away from scraping for content in this sort of way. Plus, the front-end site could change at any moment and completely break your code. It’s best to stick to APIs when you can.
you sure there is no way to do it?
Anyway pretty sure i’ve done it.
local HttpService = game:GetService("HttpService")
local Module = {}
function Module:Define(String)
local UniformResourceLocator = "https://www.dictionary.com/browse/" .. String
local GetAsync = HttpService:GetAsync(UniformResourceLocator)
local String2 = string.match(GetAsync, "content=%b\"\"")
String2 = string.gsub(String2, "content=", "")
String2 = string.gsub(String2, " See more.", "")
return String2
end
function Module:Synonym(String)
local UniformResourceLocator = "https://www.thesaurus.com/browse/" .. String
local GetAsync = HttpService:GetAsync(UniformResourceLocator)
local Table = string.split(GetAsync, "><a font-weight=\"inherit\" href=\"/browse/")
local Table2 = {}
for Number, String in pairs(Table) do
local String2 = string.match(String, "%w+")
if String2 ~= "DOCTYPE" then
table.insert(Table2, String2)
end
end
local MathRandom = math.random(1, #Table2)
return Table2[MathRandom]
end
return Module