Search in string with regular expression

M

Micke Micke

Hi all

I'm trying to build a new string from a existing one.

The original string is: my frog name is froggy
I will pull out only: frog froggy

I have tried the following:
----

str1 = 'my frog name is froggy'
str2 = str1[/frog|froggy/]

puts str2

---

This will print only the string: frog

I can get the information with this code:
----

str1 = 'my frog name is froggy'
str2 = str1[/frog] + " " + str1[/froggy/]

puts str2
----

This will print the string: frog froggy
But i think it is possible to solve this in a better way, Any
suggestions ?


// Micke
 
L

Li Chen

Micke said:
Hi all

I'm trying to build a new string from a existing one.

The original string is: my frog name is froggy
I will pull out only: frog froggy


C:\Users\Alex>irb
irb(main):001:0> str1 = 'my frog name is froggy'
=> "my frog name is froggy"
irb(main):002:0> str2=str1.scan( /froggy|frog/ )
=> ["frog", "froggy"]
irb(main):003:0> str3=str1.scan( /frog|froggy/ )
=> ["frog", "frog"]
irb(main):004:0>


Li
 
R

Robert Dober

Hi all

I'm trying to build a new string from a existing one.

The original string is: my frog name is froggy
I will pull out only: frog froggy

I have tried the following:
Maybe
str2 =3D str1.scan( /\bfrog\w*\B/ ).join(" ")
that is if you want to match frog at the start of words only (\b) and
assure charcters of your choice up to the end of the word? As \w might
be a little bit too permissive, in 1.9 you might prefer
str2 =3D str1.scan( /\bfrog[[:alpha:]]*\B/ ).join(" ")
and all those nice variations ;)

HTH
Robert
puts str2

---

This will print only the string: frog

I can get the information with this code:
----

str1 =3D 'my frog name is froggy'
str2 =3D str1[/frog] + " " + str1[/froggy/]

puts str2
----

This will print the string: frog froggy
But i think it is possible to solve this in a better way, Any
suggestions ?


// Micke


--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exup=E9ry]
 
H

Harry Kakueki

Maybe
=A0str2 =3D str1.scan( /\bfrog\w*\B/ ).join(" ")
that is if you want to match frog at the start of words only (\b) and
assure charcters of your choice up to the end of the word? As \w might
be a little bit too permissive, in 1.9 you might prefer
=A0 str2 =3D str1.scan( /\bfrog[[:alpha:]]*\B/ ).join(" ")
and all those nice variations ;)

HTH
Robert
Or maybe, :)

str2 =3D str1.scan( /\bfrog\w*\b/ ).join(" ")

Harry

--=20
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby/list.html
 
R

Robert Dober

Maybe
str2 =3D str1.scan( /\bfrog\w*\B/ ).join(" ")
that is if you want to match frog at the start of words only (\b) and
assure charcters of your choice up to the end of the word? As \w might
be a little bit too permissive, in 1.9 you might prefer
str2 =3D str1.scan( /\bfrog[[:alpha:]]*\B/ ).join(" ")
and all those nice variations ;)

HTH
Robert
Or maybe, :)

str2 =3D str1.scan( /\bfrog\w*\b/ ).join(" ")
No it was not a typo, I thought that \B was matching at the end of the
word, I did not test with a frog at the end.
I stand corrected \B does not have a special meaning and \b is just a
word boundary.

Thx
Robert
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby/list.html


--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exup=E9ry]
 
R

Robert Dober

Maybe
str2 = str1.scan( /\bfrog\w*\B/ ).join(" ")
that is if you want to match frog at the start of words only (\b) and
assure charcters of your choice up to the end of the word? As \w might
be a little bit too permissive, in 1.9 you might prefer
str2 = str1.scan( /\bfrog[[:alpha:]]*\B/ ).join(" ")
and all those nice variations ;)

HTH
Robert
Or maybe, :)

str2 = str1.scan( /\bfrog\w*\b/ ).join(" ")
No it was not a typo, I thought that \B was matching at the end of the
word, I did not test with a frog at the end.
I stand corrected \B does not have a special meaning and \b is just a
word boundary.
I mean \B is not a word boundary and \b is a word boundary and I
wonder WHAT I did test???
Sorry
R.
 
D

David A. Black

Hi --

Maybe
str2 = str1.scan( /\bfrog\w*\B/ ).join(" ")
that is if you want to match frog at the start of words only (\b) and
assure charcters of your choice up to the end of the word? As \w might
be a little bit too permissive, in 1.9 you might prefer
str2 = str1.scan( /\bfrog[[:alpha:]]*\B/ ).join(" ")
and all those nice variations ;)

HTH
Robert
Or maybe, :)

str2 = str1.scan( /\bfrog\w*\b/ ).join(" ")
No it was not a typo, I thought that \B was matching at the end of the
word, I did not test with a frog at the end.
I stand corrected \B does not have a special meaning and \b is just a
word boundary.

\B is the opposite of \b, i.e., "not at a word boundary". So:
/\B./.match("word")[0]
=> "o"


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)
 

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

No members online now.

Forum statistics

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

Latest Threads

Top