String.gsub everything until first instance of character

I have a gsub function that gets the asset of a string in a require

e.g.

local str = "require(4236423642)" -- Random asset id

local assetId = str:match("require%((.*)%)")

-->> 4236423642

but when i return a table of functions and try this:

local str = "require(4236423642).new()" -- Random asset id

local assetId = str:match("require%((.*)%)")

-->> 4236423642).new(

How can I get every character until the first instance of the closing bracket appears? So it would return 4236423642 instead of 4236423642).new(

Just replace

local assetId = str:match("require%((.*)%)")

With this:

local assetId = str:match("require%((%d+)%)")

Now it returns nil?

chaaaarrrrrsssssss

That’s strange. I pasted your code (and made some changes) and it worked for me.

1 Like

Oh its because my require function also uses strings so require('foo') also works

How can i modify it to also allow string stuff

Just simply do this:

local assetId = str:match("require%((%w+)%)")

Hope this helps you out!

1 Like

Sorry but that doesnt appear to work either
image

Huh. Both those worked for me… maybe because the words aren’t spaced out and gsub doesn’t recognize them. I’ve ran into this was I was trying to make a Lexer in the past.

1 Like

Wdym the words arent spaced out?

Like the words don’t have a space in between them, here’s what I mean (I kinda doubt this is the issue though:


-- Spaced out
require( 12345 )

-- NOT spaced out

require(12345)
1 Like

Instead of just using %w+ can you make a group for it? And then add ' and " to the group? I’m not familiar with regex / string commands

Not sure what you mean by “group” but here’s the documentation for the string pattern reference sheet if you wanna learn more:

1 Like

I did it with this pattern:
require%(([%w%s/\'\"]+)%)

Thanks for all the help :smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.