How do I get the word after a particular word in a Ruby string?
For example:
From:Ysxrb<abc@gmail.com>\nTo: <xyzn@gmail.com>Subject: xyzabc\nDate: Tue, 19 Jun 2012 03:26:56 -0700\nMessage-ID: <9D.A1.02635.ABB40EF4@ecout1>
I just want to get:
Ysxrb<abc@gmail.com
xyzabc
I think your question/requirement may need a bit of refinement.
You state: “How to get the word after a particular word in a ruby string?” and your example text is this : “From:Ysxrb\nTo: Subject: xyzabc\nDate: Tue, 19 Jun 2012 03:26:56 -0700\nMessage-ID: <9D.A1.02635.ABB40EF4@ecout1>”
and then you finally say that what you really want out of these string are the following words:
“‘Ysxrb’ and ‘xyzabc'”.
Will you always be parsing email text, which is what this appears to be? If so, then there are some more specific approaches you could take. For instance, in this example you could do something like this:
which would yield this:
At this point, if the word following “To” and “Subject” are what you’re after, you could simply get the first non-blank array element after each one, like this: