[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Subsituting a character conditionally

minkoo.seo@gmail.com

4/20/2006 7:34:00 AM

Hi group.

I'd like to substitute every quotation mark ` with whitespace. But, I
don't want to substitue "`" which is used for the purpose of
abbreviation.

Example>
I'm not a girl => I'm not a girl
`Hello world' => Hello world'

I've written the following for this purpose:

irb(main):003:0> s = "they can't prove I did `hello world'"
=> "they can't prove I did `hello world'"
irb(main):004:0> loc = (s =~ / `[a-z|A-Z]/)
=> 22
irb(main):005:0> s[loc + 1, 1] = ' '
=> " "
irb(main):006:0> s
=> "they can't prove I did hello world'"
irb(main):007:0>

But this seems to be unawkard because of loc = ( s =~ ...) which
contains seemingly two "="s. Is there another way to do this? If so,
please let me know.

Sincerely,
Minkoo Seo

1 Answer

Jeff Schwab

4/20/2006 2:04:00 PM

0

Minkoo Seo wrote:
> Hi group.
>
> I'd like to substitute every quotation mark ` with whitespace. But, I
> don't want to substitue "`" which is used for the purpose of
> abbreviation.

That's a backtick. "Quotation mark" usually means either a single quote
(') or double quote (").

> Example>
> I'm not a girl => I'm not a girl
> `Hello world' => Hello world'

Those examples use two different characters: the single quote and the
backtick, or back quote. If you want to handle both, you need to do so
explicitly, e.g:

def strip_opening_ticks(s)
s.gsub(/(^|\s)[`']/, '\1 ')
end

p strip_opening_ticks("I'm not a girl")
p strip_opening_ticks("`Hello world'")