# Regexp

**URL:** <https://www.ruby-forum.com/t/regexp/167955>\
**Category:** Ruby\
**Created:** [May 14, 2009, 1:01pm UTC](https://www.ruby-forum.com/t/regexp/167955 "2009-05-14T13:01:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mikael](https://www.ruby-forum.com/u/mikael)\
**Post date:** [May 14, 2009, 1:01pm UTC](https://www.ruby-forum.com/t/regexp/167955/1 "2009-05-14T13:01:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mikael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mikael](https://www.ruby-forum.com/u/mikael)\
**Post date:** [May 14, 2009, 1:48pm UTC](https://www.ruby-forum.com/t/regexp/167955/2 "2009-05-14T13:48:27Z")

</div>

simple, but for sure not perfect:

/(?:._?.)_(._?monday._?)./.match()[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

---

<div class="post-metadata">

**Author:** ![mikael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mikael](https://www.ruby-forum.com/u/mikael)\
**Post date:** [May 14, 2009, 1:50pm UTC](https://www.ruby-forum.com/t/regexp/167955/3 "2009-05-14T13:50:55Z")

</div>

On May 14, 7:01 am, Mikael Björkegren [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![mikael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mikael](https://www.ruby-forum.com/u/mikael)\
**Post date:** [May 14, 2009, 1:58pm UTC](https://www.ruby-forum.com/t/regexp/167955/4 "2009-05-14T13:58:11Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![mikael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mikael](https://www.ruby-forum.com/u/mikael)\
**Post date:** [May 14, 2009, 2:02pm UTC](https://www.ruby-forum.com/t/regexp/167955/5 "2009-05-14T14:02:33Z")

</div>

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”
