Another RegExp question

J

J. mp

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,
 
D

David A. Black

Hi --

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.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
H

Harry

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,

Will this work ?

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

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

Harry
 
S

Sebastian Hungerecker

J. mp said:
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.
 
R

Robert Klemme

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://ruby-doc.org/

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

Kind regards

robert
 
H

Harry

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
 
D

doug meyer

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"

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top