[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Mikael Björkegren

5/14/2009 11:02:00 AM

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

4 Answers

Mark Thomas

5/14/2009 11:47:00 AM

0

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"

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

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

-- Mark.



Clemens Wyss

5/14/2009 11:49:00 AM

0

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

Mikael Björkegren

5/14/2009 11:58:00 AM

0

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

Mikael Björkegren

5/14/2009 12:03:00 PM

0

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