Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Another RegExp question

Reply
Thread Tools

Another RegExp question

 
 
J. mp
Guest
Posts: n/a
 
      03-18-2007
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-forum.com/.

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      03-18-2007
Hi --

On 3/18/07, 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?


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)

 
Reply With Quote
 
 
 
 
Harry
Guest
Posts: n/a
 
      03-18-2007
On 3/18/07, J. mp <> 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-forum.com/.
>
>


Will this work ?

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

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

Harry


--

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

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      03-18-2007
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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-18-2007
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://ruby-doc.org/

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

Kind regards

robert
 
Reply With Quote
 
Harry
Guest
Posts: n/a
 
      03-18-2007
J. mp <> 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_...ml#String.gsub

Harry

--

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

 
Reply With Quote
 
Harry
Guest
Posts: n/a
 
      03-19-2007
On 3/18/07, J. mp <> 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/list.html
Japanese Ruby List Subjects in English

 
Reply With Quote
 
doug meyer
Guest
Posts: n/a
 
      03-20-2007
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 <> wrote:
> On 3/18/07, J. mp <> 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/list.html
> Japanese Ruby List Subjects in English
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new RegExp().test() or just RegExp().test() Matěj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57