re = /(?:"raw":\s*")(?:.*?)([0-9]{4})(?:[^"]*")/m
str = '{ "comment": "This regex is designed to turn raw fields in CSL-JSON into year only, leaving literal CE BC?E dates alone"
{"raw": "2006"}
{
"raw": "2006 Feb"
}
{"raw": "Feb 2021"}
{"raw":"2006 Mar 12"}
{"raw":"2006-12-12"}
{"raw":"2006 02 04"}
{ "raw": "2006-12-12" }
{"raw": "350 B.C.E."}
'
subst = '"raw": "$1"'
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