# Regexp

**URL:** https://rubytalk.org/t/regexp/53391
**Category:** ruby-talk
**Created:** [14 May 2009 11:01 UTC](https://rubytalk.org/t/regexp/53391 "2009-05-14T11:01:57Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Mikael\_Bjorkegren](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@Mikael\_Bjorkegren](https://rubytalk.org/u/Mikael_Bjorkegren)
#### Post date: [14 May 2009 11:01 UTC](https://rubytalk.org/t/regexp/53391/1 "2009-05-14T11:01:57Z")

</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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Clemens\_Wyss](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@Clemens\_Wyss](https://rubytalk.org/u/Clemens_Wyss)
#### Post date: [14 May 2009 11:48 UTC](https://rubytalk.org/t/regexp/53391/2 "2009-05-14T11:48:31Z")

</div>

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
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Mark\_Thomas](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@Mark\_Thomas](https://rubytalk.org/u/Mark_Thomas)
#### Post date: [14 May 2009 11:50 UTC](https://rubytalk.org/t/regexp/53391/3 "2009-05-14T11:50:15Z")

</div>

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

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

-- Mark.

> **···**
>
> On May 14, 7:01 am, Mikael Björkegren \<mb.s...@live.se\> 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"

---

<div class="post-metadata">

### Author: ![Mikael\_Bjorkegren](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@Mikael\_Bjorkegren](https://rubytalk.org/u/Mikael_Bjorkegren)
#### Post date: [14 May 2009 11:58 UTC](https://rubytalk.org/t/regexp/53391/4 "2009-05-14T11:58:13Z")

</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"
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Mikael\_Bjorkegren](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@Mikael\_Bjorkegren](https://rubytalk.org/u/Mikael_Bjorkegren)
#### Post date: [14 May 2009 12:02 UTC](https://rubytalk.org/t/regexp/53391/5 "2009-05-14T12:02:35Z")

</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"
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
