rails, finding a string and truncating a text block
Given a text block like
yada yadayada yadayada yadayada yadayada yadayada yadayada yadayada yadayada yadayada yadayada yadayada yadayada yadayada yadayada yadayada yadayada yadayada yada开发者_高级运维yada yadayada yadayada yadayada yadayada yadayada yada
On Tue, Nov 16, 2010 at 9:23 PM, Tekkub <tender+xxxxxxx@tenderapp.com> wrote:
With Rails3, I'd like to know, given a text block, how to find a string like:
On Tue, Nov 16, 2010 at 9:23 PM, Tekkub <tender+xxxxxxx@tenderapp.com> wrote:
And then create a new variable with just the text above that matched string.
Thanks
...
Ideas: 1. First need to generate that string give the day, month:
On Tue, Nov 16, 2010 at 9:23 PM, Tekkub <tender+xxxxxxx@tenderapp.com> wrote:
- Use that string to then do a FIND? and some how apply a LEFT?
So, you could do this using the scan()
method from ruby's String
class and a regex. This method is brittle, because it relies on the fact that On
will always start the string you are looking for, and a :
will always end it. But, if you say that "it should," then this will work well. This code sample assumes that @emailreply
is an instance variable containing the entire body of the email reply as a string, and will return an array of 1 containing the extracts. scan()
returns an array of all the regex matches from the string.
def extract_info
extracts = @emailreply.scan(/On.*?:/)
extracts
end
精彩评论