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

59 views Asked by At

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?

1

There are 1 answers

1
Yongqi Z On BEST ANSWER

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