01 | <?xml version = "1.0" encoding = "utf-8" ?> |
02 | <roblox xmlns:xmime = "http://www.w3.org/2005/05/xmlmime" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "http://www.roblox.com/roblox.xsd" version = "4" > |
03 | <External>null</External> |
04 | <External> nil </External> |
05 | <Item class = "BodyColors" > |
06 | <Properties> |
07 | <int name = "HeadColor" > 24 </int> |
08 | <int name = "LeftArmColor" > 24 </int> |
09 | <int name = "LeftLegColor" > 24 </int> |
10 | <string name = "Name" >Body Colors</string> |
11 | <int name = "RightArmColor" > 24 </int> |
12 | <int name = "RightLegColor" > 24 </int> |
13 | <int name = "TorsoColor" > 1003 </int> |
14 | <bool name = "archivable" > true </bool> |
15 | </Properties> |
16 | </Item> |
17 | </roblox> |
I need a string manipulation method to print every line containing the int tag.
A full XML parser is a large task. But, we can probably assume certain things about format -- for instance, that each tag is on its own line.
In that case, it's pretty easy to split lines up using :gmatch
01 | -- `xml` is the string containing XML |
02 |
03 | for line in xml:gmatch( "[^\n]+" ) do |
04 | -- The pattern: |
05 | -- \n -- newline |
06 | -- [^\n] -- any character not the newline |
07 | -- [^\n]+ -- a (longest) sequence of at least 1 non-newline character |
08 | if line:lower():match( "^%s*<int" ) then |
09 | -- The pattern: |
10 | -- ^ -- at the beginning of the line, |
11 | -- %s* -- any amount of (optional) whitespace |
12 | -- <int -- The literal characters `int` |
13 | print (line) |
14 | end |
15 | end |
EDIT: Thank you PiggyJingles, I wrote "\s"
when I meant to write "%s"
(\s is used by JavaScript; %s by Lua)
1 | --<int name=\"..\">...</int> finds every line with this beginning |
2 | --(%w+) returns the value of the name |
3 | --(%d+) returns the value inside the tag |
4 | for key, int in string.gmatch(str, "<int name=\"(%w+)\">(%d+)</int>" ) do |
5 | print (key, int); |
6 | end |
key is the name, int is the integer in the middle of the tags