Forum: Ruby Regexp

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F0feb4c0d33a8e1da974df1c8a8d5299?d=identicon&s=25 Mikael Björkegren (sveaspade)
on 2009-05-14 13:01
Hello!

Anyone who can help me with this. If i have a string and want to take
out a sub string of that by regexp.

 1. string = "Today is it monday after work. blah blah."
or
 2. string2 = "monday morning. Nice day today."

And i would like to get a certain line with the word monday in it.
Like results below.

 1. result = "Today is it monday after work"
 2. result2 = "monday morning"

Thanks
Svea
Bfa7bded551b934e38d141f86281447f?d=identicon&s=25 Clemens Wyss (clemens)
on 2009-05-14 13:48
simple, but for sure not perfect:

/(?:.*?\.)*(.*?monday.*?)\./.match(<your string>)[1]

e.g.:
/(?:.*?\.)*(.*?monday.*?)\./.match('asdasd . asdas dasd .it is monday
morning. hello world')[1]
=> "it is monday morning"


Regards
Clemens

Mikael Björkegren wrote:
> Hello!
>
> Anyone who can help me with this. If i have a string and want to take
> out a sub string of that by regexp.
>
>  1. string = "Today is it monday after work. blah blah."
> or
>  2. string2 = "monday morning. Nice day today."
>
> And i would like to get a certain line with the word monday in it.
> Like results below.
>
>  1. result = "Today is it monday after work"
>  2. result2 = "monday morning"
>
> Thanks
> Svea
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2009-05-14 13:50
(Received via mailing list)
On May 14, 7:01 am, Mikael Björkegren <mb.s...@live.se> wrote:
> Like results below.
>
>  1. result = "Today is it monday after work"
>  2. result2 = "monday morning"

So you want the sentence, not including the period? What if there is a
preceding sentence?

Try
puts string[/[^.]*[Mm]onday[^.]*/]

-- Mark.
F0feb4c0d33a8e1da974df1c8a8d5299?d=identicon&s=25 Mikael Björkegren (sveaspade)
on 2009-05-14 13:58
Thanks for the fast reply.
Works nice for stripping the last part but not the first.
I get the lines before also.

/(?:.*?\.)*(.*?monday.*?)\./.match('asdasd . asdas dasd .it is monday
morning. hello world')
=> asdasd . asdas dasd .it is monday morning.

> e.g.:
> /(?:.*?\.)*(.*?monday.*?)\./.match('asdasd . asdas dasd .it is monday
> morning. hello world')[1]
> => "it is monday morning"
F0feb4c0d33a8e1da974df1c8a8d5299?d=identicon&s=25 Mikael Björkegren (sveaspade)
on 2009-05-14 14:02
Never mind i forgott [1]
Thanks again

> /(?:.*?\.)*(.*?monday.*?)\./.match('asdasd . asdas dasd .it is monday
> morning. hello world')
> => asdasd . asdas dasd .it is monday morning.
>
>> e.g.:
>> /(?:.*?\.)*(.*?monday.*?)\./.match('asdasd . asdas dasd .it is monday
>> morning. hello world')[1]
>> => "it is monday morning"
This topic is locked and can not be replied to.