The lookahead is not necessary in this case. I improved your dollar matching slightly to also match with commas in the number, e.g., $1,000.00
, and without any commas / decimal points, e.g., $10
.
regex = /cart total is (greater|less) than \$?((?:\d+,?)+(?:.\d+)?)/
strings = [
"cart total is greater than 5.00",
"cart total is less than $1,500,000.00",
"cart total is greater than $5"
]
strings.each do |string|
p string.match(regex)
end
#<MatchData "cart total is greater than 5.00" 1:"greater" 2:"5.00">
#<MatchData "cart total is less than $1,500,000.00" 1:"less" 2:"1,500,000.00">
#<MatchData "cart total is greater than $5" 1:"greater" 2:"5">