LoginSignup
3
3

More than 5 years have passed since last update.

文字列の分割

Posted at

カンマ区切りのデータを手軽に切り出す、split関数みたいなのがLuaにほしいなと思ったのですが、
逆に、カンマでない部分を切り出してしまえば良いと気付きました。
数字や英文字のつながっている部分を抜き出すので、スペース区切りでもいいです。

s = "ab,1,23"

-- 英文字
for n in s:gmatch("%a+") do print(n) end 

-- 数字
for n in s:gmatch("%d+") do print(n) end 

-- 英数字
for n in s:gmatch("[%a%d]+") do print(n) end 

gmatchの+を除けば、一文字単位で切り出してくれます。

s = "123456"

for n in s:gmatch("%d") do print(n) end 
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3