I am trying to match any number 1-31 (inclusively).
This is the closest I have:
([1-9]|[12]\d|3[01])
But numbers like 324 are accepted.
Any chance there's a regex out there that can capture just 1-31?
I am trying to match any number 1-31 (inclusively).
This is the closest I have:
([1-9]|[12]\d|3[01])
But numbers like 324 are accepted.
Any chance there's a regex out there that can capture just 1-31?
Depending on what you are really trying to do, or to communicate with your code, it may make more sense to simply extract all integers and reject those outside your desired range. For example:
str = '0 1 20 31 324'
str.scan(/\d+/).map(&:to_i).reject { |i| i < 1 or i > 31 }
#=> [1, 20, 31]
Try with this one:/^([0-9]|1[0-9]|2[0-9]|3[01])$/
Here an example:
str = STDIN.gets.chomp
if str =~ /^([0-9]|1[0-9]|2[0-9]|3[01])$/
puts "Match!"
else
puts "No match!"
end
Here's one:
/^(#{(1..31).to_a * '|'})$/
#=> /^(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)$/