re = /^\s*
(?:
(?P<date>
(?:[\[<\(]\s*)? # opening bracket
(?:
# date time
(?:
# date
(?:
(?: # matches mm-dd-yyyy mm-dd-yy mm-dd
\d{1,2}-\d{1,2}
(?:
-
(?:\d{4}|\d{2})
)?
)
|
(?: # matches mm\/dd\/yyyy mm\/dd\/yy mm\/dd
\d{1,2}\/\d{1,2}
(?:
\/
(?:\d{4}|\d{2})
)?
)
)
\s+
# time
\d{1,2}:\d{2}
(?::\d{2})?
(?:
\s*
(?:AM|PM)
)?
)
|
# time date
(?:
# time
\d{1,2}:\d{2}
(?::\d{2})?
(?:
\s*
(?:AM|PM)
)?
\s+
# date
(?:
(?: # matches mm-dd-yyyy mm-dd-yy mm-dd
\d{1,2}-\d{1,2}
(?:
-
(?:\d{4}|\d{2})
)?
)
|
(?: # matches mm\/dd\/yyyy mm\/dd\/yy mm\/dd
\d{1,2}\/\d{1,2}
(?:
\/
(?:\d{4}|\d{2})
)?
)
)
)
|
# time only
(?:
\d{1,2}:\d{2}
(?::\d{2})?
(?:
\s*
(?:AM|PM)
)?
)
)
(?:\s*[\)>\]])? # closing bracket
)
\s+
)?
(?P<name>
(?:
(?:<\s*[^\s:>]+\s*>) # match <Username>
|
(?:[^\s:]+:) # match Username:
)
)
\s+
(?P<message>.*)
$/xm
str = '<11:54 PM> Sirenfal: Asdf
[11:54] <Sirenfal> Asdf
11:54 Sirenfal: Asdf
[1/1/06 11:54 AM] Sirenfal: asdf
<11:54> Asdf'
# 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