re = /(?<=^(?:(?:Lung|LUNG) RADS:?(?: CATEGORY:?| [cC]ategory:?)? ?|LUNGRADS? |LUNG-RAD ))(?:\d[A-Z]?|[A-Z])(?![A-Z])/m
str = 'LUNG RADS CATEGORY 2
Lung RADS category 1,
Lung RADS category 2.
LUNG RADS CATEGORY 1:
Lung RADS 3S.
Lung RADS Category 1:
Lung RADS Category 1 (S):
LUNG RADS CATEGORY 1 S
Lung RADS category: 2S.
Lung RADS: 2C
Lung RADS category 4B,
Lung RADS category 1S.
Lung RADS: 3.
Lung RADS category I
Lung RADS 2
LUNG RADS CATEGORY:I
LUNGRADS 2
LUNGRAD 2
LUNG-RAD 3
'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
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