PDA

View Full Version : Request For Assistance - Parsing Strings



dulux-oz
October 3rd, 2015, 04:06
Hi Guys,

I've gone through the documentation and worked on a few things over the last several days, but I just can't seem to get the logic right on the lua-regex stuff to do what I need - I really need someone's help - please!

The Problem
------------
To parse the following example strings and separate the prefix(s) (if it exists), the numeric(s) (using tonumber()), and the suffix(s) (if it exists).

Example Strings & Desired Results
--------------------------------
$40 => sPrefix="$", nAmount=40, sSuffix=""
$40.40 => sPrefix="$", nAmount=40.4, sSuffix=""
40Dollars => sPrefix="", nAmount=40, sSuffix="Dollars"
40 Dollars => sPrefix="", nAmount=40, sSuffix="Dollars"
40.4 Dollars => sPrefix="", nAmount=40.4, sSuffix="Dollars"
40 => sPrefix="", nAmount=40, sSuffix=""

And the hard ones:
£2.3s.6d => sPrefix1="£", nAmount1=2, sSuffix1="", sPrefix2="", nAmount2=3, sSuffix2="s", sPrefix3="", nAmount3=6, sSuffix3="d"
1/- => sPrefix1="", nAmount1=1, sSuffix1="/-"
2/6 => sPrefix1="", nAmount1=2, sSuffix1="/", sPrefix2="", nAmount2=6, sSuffix2=""
£1.10s.- => sPrefix1="£", nAmount1=1, sSuffix1="", sPrefix2="", nAmount2=10, sSuffix2="s"
£1/19/11.75d => sPrefix1="£", nAmount1=1, sSuffix1="", sPrefix2="", nAmount2=19, sSuffix2="", sPrefix3="", nAmount3=11.75, sSuffix3="d"

The Working Solution (so far)
----------------------------


if string.match(sAmount,"%d+%.%d+") then
nAmount1,sSuffix1 = string.match(sAmount,"(%d+%.%d+)%s*(%a+)");
elseif string.match(sAmount,"%d+") then
nAmount1,sSuffix1 = string.match(sAmount,"(%d+)%s*(%a+)");
end
return nAmount1,sSuffix1


Thanks in advance