Search in string with regular expression

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

Micke M. wrote:

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

On 7/15/09, Micke M. [email protected] wrote:

str1 = ‘my frog name is froggy’
str2 = str1[/frog|froggy/]
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 :wink:

HTH
Robert

str1 = ‘my frog name is froggy’
// Micke

Posted via http://www.ruby-forum.com/.


Toutes les grandes personnes ont d’abord été des enfants, mais peu
d’entre elles s’en souviennent.

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

[Antoine de Saint-Exupéry]

Or maybe, :slight_smile:

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

Harry

On 7/15/09, Robert D. [email protected] wrote:

Robert

Or maybe, :slight_smile:

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.

On 7/15/09, Harry K. [email protected] wrote:

Or maybe, :slight_smile:

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.

Thx
Robert

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


Toutes les grandes personnes ont d’abord été des enfants, mais peu
d’entre elles s’en souviennent.

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

[Antoine de Saint-Exupéry]

Hi –

On Wed, 15 Jul 2009, Robert D. wrote:

Robert

Or maybe, :slight_smile:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs