문제

I'm trying to match for two types of strings. I wish to capture both of them, but I can only capture one so far.

function roll(input)
  min, high = string.match(input, '(%d+)-(%d+)');

  return min, high;
end

The input strings are: 10-100 and 10

My first string returns as expected but my second (the single digit) returns nil/match not found.

I wish to check if the second part of the pattern is included or not, as it should always print out my min variable.

도움이 되었습니까?

해결책

Easiest is to do it separately:

function roll(input)
  local min,high = string.match(input, '(%d+)-(%d+)')
  if min == nil then 
    min = string.match(input, '(%d+)')
  end
  return min, high
end

print(roll '10')
print(roll '10-100')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top