Skip to content

Instantly share code, notes, and snippets.

@tompng
Created February 10, 2024 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tompng/fe4f39bf75f4d777e63a2c87332f7c5e to your computer and use it in GitHub Desktop.
Save tompng/fe4f39bf75f4d777e63a2c87332f7c5e to your computer and use it in GitHub Desktop.
def sum_reg(a, b, fallback = nil)
fragments = (0..18).map do |sum|
".*\\k<#{a}>.{#{sum/2+5}}_.{#{sum-sum/2+5}}\\k<#{b}>.*-.{#{sum%10}}"
end
fragments << ".*-.{#{fallback}}" if fallback
"(?:#{fragments.join('|')})"
end
S = '9876543210___________0123456789-0123456789'
reg = /^((?=.*(\3|0))(?=.*(\4|1))(?=#{sum_reg('2', '3')}(.))){5}/
puts(reg)
p reg.match S
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment