Lukas
Lukas

Reputation: 103

Lua splitting a string without separators

Is there an easy way to split a string like this:

M34a79 or M2ab943 or M4c4

into

M,34,a,79 or M,2,ab,943 or M,4,c,4

without any separators?

Upvotes: 2

Views: 189

Answers (3)

Lukas
Lukas

Reputation: 103

Solution:

http://www.coronalabs.com/blog/2013/04/16/lua-string-magic/

function string:split( inSplitPattern, outResults )
 ...
end

function val(x)
 x = x:gsub( "(%d)(%a)", "%1,%2" )
 x = x:gsub( "(%a)(%d)", "%1,%2" )

 Table = string.split(x,",")
 for i = 1, #Table do
   print( Table[i] ) 
 end
end

val("M3a5") 

returns M 3 a 5

Upvotes: 0

Philipp Gesang
Philipp Gesang

Reputation: 526

If you don’t mind using the LPEG library:

local lpeg            = require "lpeg"
local C, Ct, P, R     = lpeg.C, lpeg.Ct, lpeg.P, lpeg.R
local lpegmatch       = lpeg.match

local extract
do
  local digit     = R"09"
  local lower     = R"az"
  local comma     = P","
  local space     = P" "
  local schema    = Ct(   C(P"M")
                        * (digit^1 / tonumber)
                        * C(lower^1)
                        * (digit^1 / tonumber))
  local extractor = Ct((schema + 1)^0)
  extract = function (str)
    return lpegmatch (extractor, str)
  end
end

This will match all sequences of characters of the input that consist of (in that order)

  • the letter M,
  • a sequence of 1 or more decimal digits,
  • a sequence of 1 or more lowercase characters, and
  • another sequence of 1 or more decimal digits.

When processing the input each match is put in a subtable, the digits are converted to Lua numbers on the fly. Since the question requested it, the leading M is included in the entries.

Usage example:

local data = extract [[M34a79 or M2ab943 or M4c4]]

for i = 1, #data do
  local elm = data[i]
  print (string.format ("[%d] = { [1] = %q, [2] = %d, [3] = %q, [4] = %d },",
                        i, table.unpack (elm)))
end

Output:

[1] = { [1] = "M", [2] = 34, [3] = "a", [4] = 79 },
[2] = { [1] = "M", [2] = 2, [3] = "ab", [4] = 943 },
[3] = { [1] = "M", [2] = 4, [3] = "c", [4] = 4 },

Upvotes: 0

Kogitsune
Kogitsune

Reputation: 415

You can do it with a pair of gsub calls:

x = "M34a79 or M2ab943 or M4c4"

x, _ = x:gsub( "(%d)(%a)", "%1,%2" )
x, _ = x:gsub( "(%a)(%d)", "%1,%2" )

print( x )

M,34,a,79 or M,2,ab,943 or M,4,c,4

Might not work in all cases, but does work on your example.

Upvotes: 4

Related Questions