Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

String Patterns

Asked by 10 years ago

I'm really struggling with this.. I'm trying to get only the first 2 per line in the string printed but it doesn't work perfectly

local pattern = "(%w+)%s?(%w+).-%d+"
local get = [[Start Tackle  Normal  Physical    50  100%     35
Start   Harden  Normal  Status  ?   ?%   30
Start   Headbutt    Normal  Physical    70  100%     15]]

for level, move in get:gmatch(pattern) do
    print(level.." "..move)
end

Here is my output:

Start Tackle
10 0
Start Harden
Start Headbutt
10 0

It should only be

Start Tackle
Start Harden
Start Headbutt

2 answers

Log in to vote
1
Answered by
MrNicNac 855 Moderation Voter
10 years ago

This is what you need to produce the results you want.

local pattern = "(%w+)%s?(%w+).-\n"
local get = [[Start Tackle  Normal  Physical    50  100%     35
Start Harden  Normal  Status  ?   ?%   30
Start Headbutt    Normal  Physical    70  100%     15
]]

for level, move in get:gmatch(pattern) do
    print(level.." "..move)
end

The output

Start Tackle
Start Harden
Start Headbutt
0
oh thanks! I didn't know \n could be used in Lua! chairman154 10 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

the reason it does this is because it has digits to get rid of digits just change the pattern to this

pattern = "(%w+)%s?(%w+)"

if you use the %d then it will also include digits

0
my output is Start Tackle Normal Physical 50 100 35 Start Harden Normal Statu s 30 Start Headbutt Normal Physical 70 10 0 1 5 chairman154 10 — 10y
0
the thing is i also dont know what this code is trying to accomplish maybe if you edit it up a bit and describe what each Integer is supposed to mean DragonSkyye 517 — 10y
0
I told you want I want though... I said I just want to print the first 2 values of each line of the string chairman154 10 — 10y

Answer this question