Search code examples
lualua-patterns

How to prohibit letter input, only allow numbers in Lua?


I need to determine if a string is in the format I want. What I want is number|number|string,such as 1|2|abc. I tried this

if string.match( my_str,"[%d]|[1-2]|.") then 
  print("yes")
end

But I also got yes for a1|2|abc, which is not what I want. How to write regular expressions to ensure that the first few digits can only be numbers?


Solution

  • Thank @Nick, I use ^%d|[1-2]|.+$ works fine.