string = "First Name: John Last Name: Doe"
string2 = "First Name: John Last Name: Doe de Sour"
regex = (First Name|Last Name): ([A-Za-z]+)
How can I modify my regex to also include “de Sour”? If I add a space like so:
regex = (First Name|Last Name): ([A-Za-z ]+)
Then it’ll capture “Last Name” as well. The string is derived from the body contents of an email, so it’s important that I search for First Name and Last Name explicitly to get the contents after the “:”.
Expected return: ["First Name", "John"], ["Last Name", "De Sour"]
Important because I’ll be turning this into a Hash in Ruby.
I’d recommend turning this into a single regex that captures first name and last name at the same time (in different capture groups).
You’ll have first name in capture group 1, last name in capture group 2. No chance of confusion with “first name” or “last name” since they’ve been explicitly called out.
See it in action: http://rubular.com/r/qfA68b8PO5