[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Another RegExp question

J. mp

3/18/2007 11:41:00 AM

Hi again, I'm allways here to ask how to use regular expressions.
what I want now is to replace chars from a string with empty spaces

sample = "thi's is a tes<>%$#!|t"

what I want is use sample.sub(\['<>%$#!|]\,"")
and the outcome shoudl be

"this is a test"

But I only can replace the first occurence of the chars specified in the
reg exp

How can I replace all occurences?

Thnaks,

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

10 Answers

dblack

3/18/2007 12:02:00 PM

0

Hi --

On 3/18/07, J. mp <joaomiguel.pereira@gmail.com> wrote:
> Hi again, I'm allways here to ask how to use regular expressions.
> what I want now is to replace chars from a string with empty spaces
>
> sample = "thi's is a tes<>%$#!|t"
>
> what I want is use sample.sub(\['<>%$#!|]\,"")
> and the outcome shoudl be
>
> "this is a test"
>
> But I only can replace the first occurence of the chars specified in the
> reg exp
>
> How can I replace all occurences?

Use gsub instead of sub.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning...)
(See what readers are saying! http://www.r.../r...)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.r...)

Harry

3/18/2007 12:02:00 PM

0

On 3/18/07, J. mp <joaomiguel.pereira@gmail.com> wrote:

> sample = "thi's is a tes<>%$#!|t"
>
> what I want is use sample.sub(\['<>%$#!|]\,"")
> and the outcome shoudl be
>
> "this is a test"
>
> But I only can replace the first occurence of the chars specified in the
> reg exp
>
> How can I replace all occurences?
>
> Thnaks,
>
> --
> Posted via http://www.ruby-....
>
>

Will this work ?

sample = "thi's is a tes<>%$###!!!!!###!|t"

x = sample.gsub(/[\''<>%$#!\|]/,"")
puts x

Harry


--

http://www.kakueki.com/ruby...
Japanese Ruby List Subjects in English

Sebastian Hungerecker

3/18/2007 12:03:00 PM

0

J. mp wrote:
> what I want is use sample.sub(\['<>%$#!|]\,"")
> ...
> But I only can replace the first occurence of the chars specified in the
> reg exp
> How can I replace all occurences?

Use gsub instead of sub.


--
NP: Falkenbach - Homeward shore
Ist so, weil ist so
Bleibt so, weil war so

Robert Klemme

3/18/2007 12:03:00 PM

0

On 18.03.2007 12:40, J. mp wrote:
> Hi again, I'm allways here to ask how to use regular expressions.
> what I want now is to replace chars from a string with empty spaces
>
> sample = "thi's is a tes<>%$#!|t"
>
> what I want is use sample.sub(\['<>%$#!|]\,"")
> and the outcome shoudl be
>
> "this is a test"
>
> But I only can replace the first occurence of the chars specified in the
> reg exp
>
> How can I replace all occurences?

You can find it in the docs: http://rub...

Hint: There are sub, sub!, gsub and gsub!.

Kind regards

robert

Harry

3/18/2007 12:17:00 PM

0

J. mp <joaomiguel.pereira@gmail.com> wrote:
>
> But I only can replace the first occurence of the chars specified in the
> reg exp
>
> How can I replace all occurences?
>

http://www.rubycentral.com/book/ref_c_string.html#S...

Harry

--

http://www.kakueki.com/ruby...
Japanese Ruby List Subjects in English

Harry

3/19/2007 1:07:00 AM

0

On 3/18/07, J. mp <joaomiguel.pereira@gmail.com> wrote:
>
> sample = "thi's is a tes<>%$#!|t"
>
> what I want is use sample.sub(\['<>%$#!|]\,"")
> and the outcome shoudl be
>
> "this is a test"
>
> But I only can replace the first occurence of the chars specified in the
> reg exp
>
> How can I replace all occurences?
>

But, wouldn't it be easier to do something like this?

sample = "thi's is a tes<>%$###!!!!!###!|t"
y = sample.delete("#!%<'>$|")
puts y

Harry

--

http://www.kakueki.com/ruby...
Japanese Ruby List Subjects in English

doug meyer

3/20/2007 1:07:00 AM

0

If you want, you could tell gsub to replace everything that is not a-z
or A-Z or a white space character

sample = "thi's is a tes<>%$#!|t"
sample.gsub(/[^a-zA-Z\s]/,"")
"this is a test"

On 3/18/07, Harry <ruby.hardware@gmail.com> wrote:
> On 3/18/07, J. mp <joaomiguel.pereira@gmail.com> wrote:
> >
> > sample = "thi's is a tes<>%$#!|t"
> >
> > what I want is use sample.sub(\['<>%$#!|]\,"")
> > and the outcome shoudl be
> >
> > "this is a test"
> >
> > But I only can replace the first occurence of the chars specified in the
> > reg exp
> >
> > How can I replace all occurences?
> >
>
> But, wouldn't it be easier to do something like this?
>
> sample = "thi's is a tes<>%$###!!!!!###!|t"
> y = sample.delete("#!%<'>$|")
> puts y
>
> Harry
>
> --
>
> http://www.kakueki.com/ruby...
> Japanese Ruby List Subjects in English
>
>

Alias

5/26/2013 12:30:00 PM

0

On 5/26/2013 2:13 PM, GLOBALIST wrote:
> On Sunday, May 26, 2013 6:24:42 AM UTC-5, Alias wrote:
>> On 5/25/2013 1:21 AM, Alias wrote:
>>
>>> http://www.huffingtonpost.com/2013/05/22/pope-francis-good-atheists_n_33...
>>
>>>
>>
>>>
>>
>>> You hear that, Glob? YOUR BOSS has spoken so now you can STFU.
>>
>>>
>>
>>
>>
>> No comment from Glob, the man who hates atheists. Why not, Glob? Too
>>
>> comfortable under that little rock of yours or has your Pope
>>
>> disappointed you? I know you forgive his overseeing of selling babies
>>
>> and not doing anything about the child molesting priests. Do you not
>>
>> forgive him for what he said about atheists?
>>
>>
>>
>> --
>>
>> Alias
>>
>>
>>
>> The only real problems are avarice, anger and stupidity.
>
> Selective reading and selective journalism
>
> He said nothing new...
>
> You missed the part "redeemed through Jesus"

No, I didn't.

> Christ died for all of mankind...old news.

False news. Some say he moved to India with Magdalena, had a slew of
kids and died of old age practicing Buddhism.

> But do you see yourself as "redeemed"?

My religion is much deeper than that. We take charge of our own destiny
and don't need a fairy tale about how someone died for our sins.

> Redemption is a heavy duty part of Christianity.

Yeah, we know, and it's false.

> I would guess you would reject that part big time.

You got it.

> Murderers in prison can do "good". Drug addicts
> can do "good". Muslims do "good". The Pope is
> merely not dismissing the good that all people
> are capable of.
>

No, unlike you, he's claiming that even atheists, if they do good, will
be saved by your make believe Jesus. "Good", BTW, is a subjective
judgment and therefore meaningless.

--
Alias

The only real problems are avarice, anger and stupidity.

GLOBALIST

5/26/2013 2:11:00 PM

0

On Sunday, May 26, 2013 7:30:27 AM UTC-5, Alias wrote:
> On 5/26/2013 2:13 PM, GLOBALIST wrote:
>
> > On Sunday, May 26, 2013 6:24:42 AM UTC-5, Alias wrote:
>
> >> On 5/25/2013 1:21 AM, Alias wrote:
>
> >>
>
> >>> http://www.huffingtonpost.com/2013/05/22/pope-francis-good-atheists_n_33...
>
> >>
>
> >>>
>
> >>
>
> >>>
>
> >>
>
> >>> You hear that, Glob? YOUR BOSS has spoken so now you can STFU.
>
> >>
>
> >>>
>
> >>
>
> >>
>
> >>
>
> >> No comment from Glob, the man who hates atheists. Why not, Glob? Too
>
> >>
>
> >> comfortable under that little rock of yours or has your Pope
>
> >>
>
> >> disappointed you? I know you forgive his overseeing of selling babies
>
> >>
>
> >> and not doing anything about the child molesting priests. Do you not
>
> >>
>
> >> forgive him for what he said about atheists?
>
> >>
>
> >>
>
> >>
>
> >> --
>
> >>
>
> >> Alias
>
> >>
>
> >>
>
> >>
>
> >> The only real problems are avarice, anger and stupidity.
>
> >
>
> > Selective reading and selective journalism
>
> >
>
> > He said nothing new...
>
> >
>
> > You missed the part "redeemed through Jesus"
>
>
>
> No, I didn't.
>
>
>
> > Christ died for all of mankind...old news.
>
>
>
> False news. Some say he moved to India with Magdalena, had a slew of
>
> kids and died of old age practicing Buddhism.
>
>
>
> > But do you see yourself as "redeemed"?
>
>
>
> My religion is much deeper than that. We take charge of our own destiny
>
> and don't need a fairy tale about how someone died for our sins.
>
>
>
> > Redemption is a heavy duty part of Christianity.
>
>
>
> Yeah, we know, and it's false.
>
>
>
> > I would guess you would reject that part big time.
>
>
>
> You got it.
>
>
>
> > Murderers in prison can do "good". Drug addicts
>
> > can do "good". Muslims do "good". The Pope is
>
> > merely not dismissing the good that all people
>
> > are capable of.
>
> >
>
>
>
> No, unlike you, he's claiming that even atheists, if they do good, will
>
> be saved by your make believe Jesus. "Good", BTW, is a subjective
>
> judgment and therefore meaningless.
>
>
>
> --
>
> Alias
>
>
>
> The only real problems are avarice, anger and stupidity.

In other words you are happy to be in that "saved" category?
Saved from what?

Alias

5/26/2013 3:45:00 PM

0

On 5/26/2013 4:11 PM, GLOBALIST wrote:
> On Sunday, May 26, 2013 7:30:27 AM UTC-5, Alias wrote:
>> On 5/26/2013 2:13 PM, GLOBALIST wrote:
>>
>>> On Sunday, May 26, 2013 6:24:42 AM UTC-5, Alias wrote:
>>
>>>> On 5/25/2013 1:21 AM, Alias wrote:
>>
>>>>
>>
>>>>> http://www.huffingtonpost.com/2013/05/22/pope-francis-good-atheists_n_33...
>>
>>>>
>>
>>>>>
>>
>>>>
>>
>>>>>
>>
>>>>
>>
>>>>> You hear that, Glob? YOUR BOSS has spoken so now you can STFU.
>>
>>>>
>>
>>>>>
>>
>>>>
>>
>>>>
>>
>>>>
>>
>>>> No comment from Glob, the man who hates atheists. Why not, Glob? Too
>>
>>>>
>>
>>>> comfortable under that little rock of yours or has your Pope
>>
>>>>
>>
>>>> disappointed you? I know you forgive his overseeing of selling babies
>>
>>>>
>>
>>>> and not doing anything about the child molesting priests. Do you not
>>
>>>>
>>
>>>> forgive him for what he said about atheists?
>>
>>>>
>>
>>>>
>>
>>>>
>>
>>>> --
>>
>>>>
>>
>>>> Alias
>>
>>>>
>>
>>>>
>>
>>>>
>>
>>>> The only real problems are avarice, anger and stupidity.
>>
>>>
>>
>>> Selective reading and selective journalism
>>
>>>
>>
>>> He said nothing new...
>>
>>>
>>
>>> You missed the part "redeemed through Jesus"
>>
>>
>>
>> No, I didn't.
>>
>>
>>
>>> Christ died for all of mankind...old news.
>>
>>
>>
>> False news. Some say he moved to India with Magdalena, had a slew of
>>
>> kids and died of old age practicing Buddhism.
>>
>>
>>
>>> But do you see yourself as "redeemed"?
>>
>>
>>
>> My religion is much deeper than that. We take charge of our own destiny
>>
>> and don't need a fairy tale about how someone died for our sins.
>>
>>
>>
>>> Redemption is a heavy duty part of Christianity.
>>
>>
>>
>> Yeah, we know, and it's false.
>>
>>
>>
>>> I would guess you would reject that part big time.
>>
>>
>>
>> You got it.
>>
>>
>>
>>> Murderers in prison can do "good". Drug addicts
>>
>>> can do "good". Muslims do "good". The Pope is
>>
>>> merely not dismissing the good that all people
>>
>>> are capable of.
>>
>>>
>>
>>
>>
>> No, unlike you, he's claiming that even atheists, if they do good, will
>>
>> be saved by your make believe Jesus. "Good", BTW, is a subjective
>>
>> judgment and therefore meaningless.
>>
>>
>>
>> --
>>
>> Alias
>>
>>
>>
>> The only real problems are avarice, anger and stupidity.
>
> In other words you are happy to be in that "saved" category?

Don't put words in my mouth that aren't there.

> Saved from what?
>

I don't need saving by anyone. I take responsibility for my own life and
don't rely on magical fairy tales like you do.

Care to reply to the other points I made or merely scurry under that
rock of yours like you do so many times.

--
Alias

The only real problems are avarice, anger and stupidity.