# How to get word from sentence

**URL:** <https://www.ruby-forum.com/t/how-to-get-word-from-sentence/185044>\
**Category:** Ruby\
**Created:** [March 12, 2010, 11:48am UTC](https://www.ruby-forum.com/t/how-to-get-word-from-sentence/185044 "2010-03-12T11:48:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 12, 2010, 11:48am UTC](https://www.ruby-forum.com/t/how-to-get-word-from-sentence/185044/1 "2010-03-12T11:48:56Z")

</div>

Hi all,

In my application i have to get the word which contains given word.

example

sentence: iam testung the aplication

i want to extract the word which starts with test

how to do this

---

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 12, 2010, 11:53am UTC](https://www.ruby-forum.com/t/how-to-get-word-from-sentence/185044/2 "2010-03-12T11:53:27Z")

</div>

On Fri, Mar 12, 2010 at 11:48 AM, Rajkumar S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,  
> In my application i have to get the word which contains given word.  
> example  
> sentence: iam testung the aplication  
> i want to extract the word which starts with test  
> how to do this

You can do it with a regular expression. Here is an example:

irb(main):001:0\> s = “i am testing the app”  
=\> “i am testing the app”  
irb(main):004:0\> s[/\btest\w\*/]  
=\> “testing”

\b → word boundary  
\w → word character

Jesus.

---

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 12, 2010, 1:10pm UTC](https://www.ruby-forum.com/t/how-to-get-word-from-sentence/185044/3 "2010-03-12T13:10:07Z")

</div>

On Fri, Mar 12, 2010 at 6:48 PM, Rajkumar S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> In my application i have to get the word which contains given word.
> 
> example
> 
> sentence: iam testung the aplication
> 
> i want to extract the word which starts with test

or use scan:

irb(main):005:0\> s=“iam testung the aplication”  
=\> “iam testung the aplication”  
irb(main):006:0\> s.scan /\btest\w\*/  
=\> [“testung”]

---

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 12, 2010, 1:23pm UTC](https://www.ruby-forum.com/t/how-to-get-word-from-sentence/185044/4 "2010-03-12T13:23:43Z")

</div>

thanxs  
how to get the first word matches the expresion if i have no of words  
which matches.

JesÃºs Gabriel y GalÃ¡n wrote:

> On Fri, Mar 12, 2010 at 11:48 AM, Rajkumar S.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi all,  
> > In my application i have to get the word which contains given word.  
> > example  
> > sentence: iam testung the aplication  
> > i want to extract the word which starts with test  
> > how to do this
> 
> You can do it with a regular expression. Here is an example:
> 
> irb(main):001:0\> s = “i am testing the app”  
> =\> “i am testing the app”  
> irb(main):004:0\> s[/\btest\w\*/]  
> =\> “testing”
> 
> \b → word boundary  
> \w → word character
> 
> Jesus.

---

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 12, 2010, 2:02pm UTC](https://www.ruby-forum.com/t/how-to-get-word-from-sentence/185044/5 "2010-03-12T14:02:13Z")

</div>

If I understand you correctly, this’ll do the trick:

irb(main):024:0\* s=“Iam testung the aplication”  
=\> “Iam testung the aplication”  
irb(main):025:0\> s[/\btest\w\*/]||s[/\w\*/]  
=\> “testung”  
irb(main):026:0\> s=“Iam running the aplication”  
=\> “Iam running the aplication”  
irb(main):027:0\> s[/\btest\w\*/]||s[/\w\*/]  
=\> “Iam”

-Rolf
