2

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?

1
  • 2
    Leave the question as originally asked and add the solution as an answer. Commented Oct 25, 2013 at 15:58

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, it works! :) Is it possible to store each of these values in variables?
Would you want it to be a table of them, like { "M", "34", "a", "79" }, { "M", "2", "ab", "943" }, { "M", "4", "c", "4" } ?
You don't need the _, just x = x:gsub( "(%d)(%a)", "%1,%2" ) is enough.
0

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 },

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.