'Problem to convert a String into a Table with pattern matching
Given is the string:
local a = "#5*$4a+02/+2-%110"
Now i want to split the following string into a table like
b[1]="5"
b[2]="*"
b[3]="$4a"
b[4]="+"
...
b[10]="%110"
My solution was
local b = {}
for part in string.gmatch(a,"[%x%$%%]+[%+%-%/%*]+") do
b[#b+1] = part
end
But i get
b[1]="5*"
b[2]="$4a+"
b[3]="02/"
...
but without the last number: %110.
Solution 1:[1]
You mean:
string.gmatch(a,"[%x%$%%]+[%+%-%/%*]+[%x%$%%]")
This result was an empty Table. I want to caclulate the formula in that string.
local a = "#5*$4a+02/+2-%110"
The problem is now, a eval like
function evalStr(str)
local f= load ('return ('..str..')')
return f()
end
can calculate this Formula only with Decimal-Digits. Here i it is mixed with Hexadecimal and Binary Digits. So i have to to convert the hexadecimal and binary digits to decimal to calculate the formula.
Solution 2:[2]
local a = "#5*$4a+02/+2-%110"
local b = {}
for part in string.gmatch(a,"[^#][%x%%%$]*[%+%-%*/]*") do
b[#b+1] = part:match("[%w%%%$]+")
b[#b+1] = part:match("[%+%-%*/]")
end
for k,v in pairs(b) do
print(v)
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | a.c.m. |
| Solution 2 | a.c.m. |
