Help with string.match()

I really suck with strings so it’d be nice for some guidance. I want to be able to match strings in a PascalCase when only part of that string is provided. Lemme explain.

I’ve looked on the devforum and read the wikis but I couldn’t understand it, nor could I find useful answers.

Say I have a table with some strings in an array

local Test = {
'Pole',
'Sector',
'Division'
}

And for example I have some variables inside the workspace like so,

local NorthPole = workspace.NorthPole
local SouthPole = workspace.SouthPole
local EastPole = workspace.EastPole
local WestPole = workspace.WestPole

-- Same goes for the Sectors and Divisions, you get the gist of it
-- It also doesn't have to be in workspace, it can just be a variable somewhere

I want to match the string in the array, so that I’d get North, South, East, and WestPole from 1 string.

If I match the string ‘Pole’ I would get every single match and it returns every character in a PascalCase preceding it.

Any tips or some help?

To make clearer,

If I have string.match(Test[1], workspace:GetChildren) or something like that,

I want it to not only print ‘Pole’ but also North, South, East, and WestPole.

Sorry if the answer is really obvious, I am a dunce with strings.

1 Like

can you explain your goal a little bit more clearly

Maybe this might help.

If I match the string Pole with every string that has the word pole inside it, the string in the table will now read every uppercase letter in a PascalCase preceding it.

If NorthPole, SouthPole, EastPole, and WestPole exists, the string will print this in the output

NorthPole
SouthPole
EastPole
WestPole

If I have this variable

TheNorthPole -- I want it to print "TheNorthPole"

If I have this variable

andThisIsTheNorthPole -- It'll only print "ThisIsTheNorthPole" because and is not part of the PascalCase string
YesandThisIsTheNorthPole -- same goes for this string

You can’t get ThisIsTheNorthPole from YesandThisIsTheNorthPole based on PascalCase since you can’t know that and is another word instead of part of Yes.

To get ThisIsTheNorthPole from andThisIsTheNorthPole you can match [A-Z][A-Za-z]*, this will return everything after the first uppercase letter that contains characters from A to z.

How would I match them all? I would use something like this? Looked on the wiki and that would get every uppercase letter. So would that be the right direction?

string:find('%u')

Also I don’t quite understand

You should explain better what you want to do, I don’t understand the question.

If you want to match multiple strings, string.match and string.find only do one, you have to use a loop.

Or If you want to match multiple characters, you can use + (one or more characters) or * (zero or more characters), for example: string.match("Hello", "l+") returns you "ll".

That’s what i should say, first explain what you don’t understand.

Say I want to create multiple instances. I want to make a bool for example

I take my object in the table which is ‘Pole’ and I loop through the workspace. I find a match and it’s NorthPole. Because the word ‘Pole’ exists in both instances. Since I want to return every character in a PascalCase, it would return NorthPole and a new boolvalue is created called ‘NorthPole’ and I find another match in the workspace called SouthPole, a new boolvalue called ‘SouthPole’ is created.

I hope that can clear up what I am trying to say.

As for the not understanding, I just don’t understand how strings work like that. And I also meant I don’t understand what you said about this

Ok then if I understand, you have a table with multiple strings, and you want to check if the name of an Instance matches one of the strings in that table, and you want it to also match the rest of the name that is PascalCase?

Yes. If that name of the Instance matches one of the strings in the table it will return the rest of that name in the PascalCase

local YourTable = {"Pole", "Sector", "Division"}

-- For every child in Workspace
for _, child in pairs(game.Workspace:GetChildren()) do
    -- For every string in YourTable
    for _, str in pairs(YourTable) do
        -- Check if name of the child matches the string
        if string.match(child.Name, str) then
            -- Then get the PascalCase
            local pascalCaseName = string.match(child.Name, "[A-Z][A-Za-z]*")
            -- Do something
            break
        end
    end
end
1 Like