Another problem with regular expression

I have an array with the following elements:

array[0] = "car"
array[1] = "bike"
array[2] = “motorcicle”

array[n] = …

And I have a string with the following text.

"Today I went to school by car. I saw… "

What I am trying to do is that everytime the program finds one of the words
from the list in the text,
it will place an astherisc * to replace that word.

But I cannot figure out how to do this because it is an array not a regular
expression.

Rob

Rob wrote:

I have an array with the following elements:

array[0] = “car”
array[1] = “bike”
array[2] = “motorcicle”

array[n] = …

And I have a string with the following text.

"Today I went to school by car. I saw… "

What I am trying to do is that everytime the program finds one of the words
from the list in the text,
it will place an astherisc * to replace that word.

But I cannot figure out how to do this because it is an array not a regular
expression.

You can interpolate it into a regex, but I don’t know how well that scales:

array =
array[0] = “car”
array[1] = “bike”
array[2] = “motorcicle”

string = "Today I went to school by car. I saw… "

p string.gsub(/#{array.join(“|”)}/o) {|s| “*” * s.size}

Short way:

array.each { |w| s.gsub!(w, "*") }

Longer, possibly more efficient way:

re = array.join('|')
s.gsub!(/(#{re})/, "*")

The second way is probably more efficient, since it can optimize the
match and do it in one pass.

HTH,

···

On Wed, 25 Jun 2003 15:27:06 +0900 “Rob” robson@magario.com wrote:

What I am trying to do is that everytime the program finds one of the words
from the list in the text,
it will place an astherisc * to replace that word.

But I cannot figure out how to do this because it is an array not a regular
expression.


Ryan Pavlik rpav@users.sf.net

“And I’m 10,000 feet up in the air. Dang it.” - 8BT

“Joel VanderWerf” vjoel@PATH.Berkeley.EDU schrieb im Newsbeitrag
news:3EF94358.7030703@path.berkeley.edu…

Rob wrote:

I have an array with the following elements:

array[0] = “car”
array[1] = “bike”
array[2] = “motorcicle”

array[n] = …

And I have a string with the following text.

"Today I went to school by car. I saw… "

What I am trying to do is that everytime the program finds one of the
words
from the list in the text,
it will place an astherisc * to replace that word.

But I cannot figure out how to do this because it is an array not a
regular
expression.

You can interpolate it into a regex, but I don’t know how well that
scales:

array =
array[0] = “car”
array[1] = “bike”
array[2] = “motorcicle”

string = "Today I went to school by car. I saw… "

p string.gsub(/#{array.join(“|”)}/o) {|s| “*” * s.size}

If you want to make sure you match words you’ll have to do

p string.gsub(/\b(#{array.join(“|”)})\b/) {|s| “*” * s.size}

Better scaling for large arrays:

string.gsub( /\b(\w+)\b/ ) {|m| array.include?( m ) ? “*” * m.length : m }

One can optimize performance by changing the array into a hash and testing
for hash.has_key? instead of array.include?

robert

Or something like

hash = {}
array.each {|e| h[e] = ‘*’ * e.length}
string.split(/\b/).map {|w| hash[w] || w}.join()

martin

···

Robert Klemme bob.news@gmx.net wrote:

Better scaling for large arrays:

string.gsub( /\b(\w+)\b/ ) {|m| array.include?( m ) ? “*” * m.length : m }

One can optimize performance by changing the array into a hash and testing
for hash.has_key? instead of array.include?

Rob wrote:

I have an array with the following elements:

array[0] = “car”
array[1] = “bike”
array[2] = “motorcicle”

array[n] = …

And I have a string with the following text.

"Today I went to school by car. I saw… "

What I am trying to do is that everytime the program finds one of the
words from the list in the text,
it will place an astherisc * to replace that word.

Maybe I’m just the cynic here, but does this sound to anyone else suspicously
like a “do my homework for me” request?

Mike Campbell wrote:

Rob wrote:

I have an array with the following elements:

Maybe I’m just the cynic here, but does this sound to anyone else suspicously
like a “do my homework for me” request?

If so, maybe we should grant Rob immunity in this case in exchange for
testifying where they give homework assignments in Ruby?

···


([ Kent Dahl ]/)_ ~ [ Kent Dahl - Kent Dahl ]/~
))_student_/(( _d L b_/ (pre-) Master of Science in Technology )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

Even better! great!

robert

“Martin DeMello” martindemello@yahoo.com schrieb im Newsbeitrag
news:f0dKa.278806$ro6.7228707@news2.calgary.shaw.ca…

Better scaling for large arrays:

string.gsub( /\b(\w+)\b/ ) {|m| array.include?( m ) ? “*” * m.length :
m }

One can optimize performance by changing the array into a hash and
testing

···

Robert Klemme bob.news@gmx.net wrote:

for hash.has_key? instead of array.include?

Or something like

hash = {}
array.each {|e| h[e] = ‘*’ * e.length}
string.split(/\b/).map {|w| hash[w] || w}.join()

martin