Is this wrong ? string manipulation (string.match)

Hello,
I’m new to string manipulation and im trying to make some extra values that come with weapons for example like this

image
|WeaponName: Level,XP,Tier|
i was able to seperate those and edit them as well
image

but the only thing i can’t figure out is searching for the whole thing inside the "|"s
the issue i’m facing is this

image
image

which keeps printing nil idk why
does anyone know a way to fix this ? thanks

What you’re trying to do is extremely confusing. Can you not just access the Value to get everything between the two horizontal lines? Why do you need to use string manipulation for this? What exactly are you attempting to do?

Given I have no clue what you’re trying to do or why you are trying to do it, the closest I could come to interpreting this is that you want to use string capturing to capture everything inside the lines.

local a = "|Katana: 15,1000,1|"
local b = a:match("%b||")
local c = b:sub(2, #b-1)

print(b) -- |Katana: 15,1000,1|
print(c) -- Katana: 15,1000,1

-- ...you can just use the raw value or string?
1 Like

Since i have an inventory data value full of weapons and all
i’m also trying to make weapons stats which can be also upgraded
for the upgrade process i want to check if “Katana” is already in inventory, if it is, it returns this image
then after changing those numbers ofc i put it back in inventory string value with all the changes ive made

the inventory value can be something like this
“|Katana: 15,1000,1||Pistol: 2,0,3|”

So I take it that what you’re attempting to do is separate sets of weaponry through those horizontal lines? For example, when you save it the string looks like what you posted but when you want to update values for a weapon, you want to split them each up? Visual aid to what I mean:

Saved string:
    "|Katana: 15,1000,1||Pistol: 2,0,3|"
Wish to split like so:
    "|Katana: 15,1000,1|"
    "|Pistol: 2,0,3|"

If the above is the case where you want to attempt to interact with items of the string in the basis of two horizontal lines, you are actually going to want to use gmatch, not match. You will also require a for loop to iterate over all matched cases.

local a = "|Katana: 15,1000,1||Pistol: 2,0,3|"

-- Also acceptable: string.gmatch(a, "%b||")
for weaponData in a:gmatch("%b||") do
	print(weaponData)
end

The following code will result in this output:

|Katana: 15,1000,1|
|Pistol: 2,0,3|

gmatch is meant to be a pattern iterator, so what we’re basically doing is taking match and instead turning it into something we can iterate over. The %b|| is a balanced capture which will match everything between the lines as well as the lines themselves. Then, we can retrieve each match of our pattern (any string that appears as |inner text|) and handle that accordingly.

Does this answer your question?

1 Like

wow yes i guess thats all i wanted to do but i didn’t want to split all of them though, just look for the string “Katana” in this string “|Katana: 15,1000,1||Pistol: 2,0,3|”, after finding it i get this |Katana: 15,1000,1|
but thx man that really helped

If you just want the Katana string, it’d be as simple as writing a match statement for the word Katana. No regex or anything.

local a = "|Katana: 15,1000,1||Pistol: 2,0,3|"
local b = "Katana"
local c = a:match(b)

print(c) -- Katana

Again, not really sure why you would need to do this if you already have the string available. I might understand a little better though: are you trying to get the whole katana set just from finding the word Katana? You should really just handle it via gmatch so you avoid any weird workarounds or anything.

Edit: I tried this problem one last time and got tacky results, honestly.
local a = "|Katana: 15,1000,1||Pistol: 2,0,3|"
local b = "|(.*)||"

local sName = a:match(b)
print(sName) -- Katana: 15,1000,1
1 Like

Well, the script you provided earlier was the one that suited me the most to be honest

local ToFind = "Katana"
local Pattern = "%d+%p+%d+%p+%d+"
local Data = "|Katana: 12,10,1||Pistol: 2,0,3|"
local Found

for weaponData in Data:gmatch("%b||") do
	if string.find(weaponData,"|"..ToFind..":") then
		Found = weaponData
		print(Found) --> |Katana: 12,10,1|
	end
end

I’m really thankful for your help though (:

EDIT: and here what i’m gonna do for the rest

if Found ~= nil then
    local WeaponStats = string.match(Found,Pattern):split(",")
    local Level = WeaponStats[1] local XP = WeaponStats[2] local Tier = WeaponStats[3]
    print("Level:"..Level..",XP:"..XP..",Tier:"..Tier) --> Level:12,XP:10,Tier:1
end
1 Like