re = /^([^\h]+)\h*(?:(OPS|EC5|CSP|PBF|PVF)(?:([^\h])\h|([^\h]{2})|\h{2}))(?:([^\h]{2})|\h{2})\h([^\h]+)\h+(.{4})\h(.{5})\h(.{10})\h/m
str = '7089 OPS 7089 1 / aar both all
7901 OPS 7901 1 / aar both all
90510 EC500 467012312312345 3 / ars both all
906101PVFMC46 701234567 12/ ars both all
90-71011CSP 46 707123457 12/ ars both all '
subst = '$1|$2$3$4|$5|$6|$7|$8|$9|'
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