how to get word from sentence

R

Rajkumar Surabhi

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
 
J

Jesús Gabriel y Galán

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

Jean G.

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"]
 
R

Rajkumar Surabhi

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

Jesús Gabriel y Galán said:
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.
 
R

Rolf Pedersen

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

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

-Rolf

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

Jes=FAs Gabriel y Gal=E1n said:
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 =3D "i am testing the app"
=3D> "i am testing the app"
irb(main):004:0> s[/\btest\w*/]
=3D> "testing"

\b --> word boundary
\w --> word character

Jesus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top