re = /((?:bs )?(?:en )?(?:np )?(?:is[o0]|DIN)\s+[-\d]+:)(.+:)?(\d{4}(?:\(\w\))?)/im
str = 'ISO 11784:1996/Amd.2:2010(E)
ISO 11784:2010(E)
ISO 11784:2010
ISO 11784-2-3-4:1996/Amd.2:2010(E)
ISO 11784-2-8-7:2010(E)
ISO 11784-5-120-78949-56-789:2010
BS EN ISO 11784:1996/Amd.2:2010(E)
EN ISO 11784:2010(E)
IS0 11784:2010
EN NP IS0 11784-2-3-4:1996/Amd.2:2010(E)
BS IS0 11784-2-8-7:2010(E)
EN NP ISO 11784-5-120-78949-56-789:2010
EN DIN 11784-5:2019(B)'
subst = '$1$3'
result = str.gsub(re, subst)
# Print the result of the substitution
puts result
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html