[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

gsub and regex

Tom Cloyd

6/18/2008 11:29:00 AM

I'm trying to replace all /\n / with /\n/ in a string, but gsub won't
allow a regex as the replacement when a match occurs. I'm getting myself
considerably confused about data types and regex, and just plain need
some help. Is there some way to do this replacement, using gsub?

Thanks,

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


2 Answers

Stefano Crocco

6/18/2008 11:31:00 AM

0

On Wednesday 18 June 2008, Tom Cloyd wrote:
> I'm trying to replace all /\n / with /\n/ in a string, but gsub won't
> allow a regex as the replacement when a match occurs. I'm getting myself
> considerably confused about data types and regex, and just plain need
> some help. Is there some way to do this replacement, using gsub?
>
> Thanks,
>
> t.

I'm not sure I understand you. Do you want to replace all newline charactes
followed by a space with a single newline character? If so, you don't need
regexps at all:

str.gsub("\n ", "\n")

I hope this helps

Stefano


Tom Cloyd

6/18/2008 11:44:00 AM

0

Stefano Crocco wrote:
> On Wednesday 18 June 2008, Tom Cloyd wrote:
>
>> I'm trying to replace all /\n / with /\n/ in a string, but gsub won't
>> allow a regex as the replacement when a match occurs. I'm getting myself
>> considerably confused about data types and regex, and just plain need
>> some help. Is there some way to do this replacement, using gsub?
>>
>> Thanks,
>>
>> t.
>>
>
> I'm not sure I understand you. Do you want to replace all newline charactes
> followed by a space with a single newline character? If so, you don't need
> regexps at all:
>
> str.gsub("\n ", "\n")
>
> I hope this helps
>
> Stefano
>
>
>
>
Man, this list is absolutely amazing. Here I am programming at 4:30 in
the morning, and get stuck, and before I can execute my program again, I
get my problem solved. I think I want to live in Ruby land the rest of
my life.

And...how could I have forgotten that in "...." \n get understood. Jeez.
So easy.

Thanks for being there.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~