re = /^(
( # GENERAL before . (Dot)
(?!\w+\?) # DO NOT MATCH if contains ?
[\w\]\)]+ # Word, ] or ) characters 1 or more times
)|
(?:\((\w+)\))| # BRACKETS ()
([\w\]\)]+(?![?*+])) # BEFORE . (Dot) with ?, * or +
)/mx
str = 'abcdef.*g[0abc]{0,5}hi # abcdef
]1234vac.*12345 # ]1234vac
)1234vac.*12345 # )1234vac
(abc)+123 # abc
12345[abcde]+12345 # 12345
123?456 # 12
123\\?456 # 123?456
# --- No Prefix ---
[A-z]+12345
(1234vac.*12345
[a-z]+'
# 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