[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Newbie Question: delete all non alphanumeric characters

Theallnighter Theallnighter

7/21/2006 5:54:00 PM

Hi all,
how can i delete all non alphanumeric characters in a string ? thanks

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

16 Answers

Logan Capaldo

7/21/2006 6:01:00 PM

0


On Jul 21, 2006, at 1:53 PM, Theallnighter Theallnighter wrote:

> Hi all,
> how can i delete all non alphanumeric characters in a string ? thanks
>
> --
> Posted via http://www.ruby-....
>

string.gsub(/[0-9a-z]+/i, '')


Tom Werner

7/21/2006 6:06:00 PM

0

Logan Capaldo wrote:
>
> On Jul 21, 2006, at 1:53 PM, Theallnighter Theallnighter wrote:
>
>> Hi all,
>> how can i delete all non alphanumeric characters in a string ? thanks
>>
>> --
>> Posted via http://www.ruby-....
>>
>
> string.gsub(/[0-9a-z]+/i, '')
>
>
>
That deletes all alphanumeric. To delete all non-alphanumeric:

string.gsub(/[^0-9a-z]/i, '')

--
Tom Werner
Helmets to Hardhats
Software Developer
tom@helmetstohardhats.org
www.helmetstohardhats.org


Logan Capaldo

7/21/2006 6:29:00 PM

0


On Jul 21, 2006, at 2:05 PM, Tom Werner wrote:

> Logan Capaldo wrote:
>>
>> On Jul 21, 2006, at 1:53 PM, Theallnighter Theallnighter wrote:
>>
>>> Hi all,
>>> how can i delete all non alphanumeric characters in a string ?
>>> thanks
>>>
>>> --
>>> Posted via http://www.ruby-....
>>>
>>
>> string.gsub(/[0-9a-z]+/i, '')
>>
>>
>>
> That deletes all alphanumeric. To delete all non-alphanumeric:
>
> string.gsub(/[^0-9a-z]/i, '')
>
> --
> Tom Werner
> Helmets to Hardhats
> Software Developer
> tom@helmetstohardhats.org
> www.helmetstohardhats.org
>
>

Doh! I'm obviously not awake yet this ---err-- afternoon.


mathew

7/21/2006 6:55:00 PM

0

Tom Werner wrote:
> Logan Capaldo wrote:
>> On Jul 21, 2006, at 1:53 PM, Theallnighter Theallnighter wrote:
>>> Hi all,
>>> how can i delete all non alphanumeric characters in a string ? thanks
>>
>> string.gsub(/[0-9a-z]+/i, '')
>>
> That deletes all alphanumeric. To delete all non-alphanumeric:
>
> string.gsub(/[^0-9a-z]/i, '')

Or string.delete("^0-9a-zA-Z")


mathew
--
<URL:http://www.pobox.com/...
My parents went to the lost kingdom of Hyrule
and all I got was this lousy triforce.

Jim Cochrane

7/21/2006 7:38:00 PM

0

On 2006-07-21, Theallnighter Theallnighter <theallnighter@gmail.com> wrote:
> Hi all,
> how can i delete all non alphanumeric characters in a string ? thanks
>

I've also just started to learn Ruby, so thought I'd reply for the practice -
Here's one solution:


------------------------------------------------------------------------
#!/usr/bin/ruby

x = "There are 2007 beans and 15234 grains of rice in this bag."
puts x
x.gsub!(/\W/, '')
puts x

------------------------------------------------------------------------

output:

There are 2007 beans and 15234 grains of rice in this bag.
Thereare2007beansand15234grainsofriceinthisbag

--

Logan Capaldo

7/21/2006 7:48:00 PM

0


On Jul 21, 2006, at 3:40 PM, Jim Cochrane wrote:

> On 2006-07-21, Theallnighter Theallnighter
> <theallnighter@gmail.com> wrote:
>> Hi all,
>> how can i delete all non alphanumeric characters in a string ? thanks
>>
>
> I've also just started to learn Ruby, so thought I'd reply for the
> practice -
> Here's one solution:
>
>
> ----------------------------------------------------------------------
> --
> #!/usr/bin/ruby
>
> x = "There are 2007 beans and 15234 grains of rice in this bag."
> puts x
> x.gsub!(/\W/, '')
> puts x
>
> ----------------------------------------------------------------------
> --
>
> output:
>
> There are 2007 beans and 15234 grains of rice in this bag.
> Thereare2007beansand15234grainsofriceinthisbag
>
> --
>
>

Well the only "problem" with that is

x = '\w includes_under_scores_too'


Jim Cochrane

7/21/2006 8:01:00 PM

0

On 2006-07-21, Logan Capaldo <logancapaldo@gmail.com> wrote:
>
> On Jul 21, 2006, at 3:40 PM, Jim Cochrane wrote:
>
>> On 2006-07-21, Theallnighter Theallnighter
>> <theallnighter@gmail.com> wrote:
>>> Hi all,
>>> how can i delete all non alphanumeric characters in a string ? thanks
>>>
>> ...
>> #!/usr/bin/ruby
>>
>> x = "There are 2007 beans and 15234 grains of rice in this bag."
>> puts x
>> x.gsub!(/\W/, '')
>> puts x
>> ...
>>
>>
>
> Well the only "problem" with that is
>
> x = '\w includes_under_scores_too'
>

Woah! Thanks for pointing that out. It looks like
http://www.ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/r...
has a bug:

\w letter or digit; same as [0-9A-Za-z]

It's missing a _.

Here's a fixed version:


#!/usr/bin/ruby

x = "There are 2007 beans_and 15234 grains of rice in this bag."
puts x
x.gsub!(/\W/, '')
puts x
x.gsub!(/\W|_/, '')
puts "fixed:"
puts x

dominique.plante@gmail.com

7/21/2006 8:10:00 PM

0

for fun, I started irb, then typed

"567576hgjhgjh&**)".gsub(/^[0-9a-z]/i, '')

It returned

67576hgjhgjh&**)

Tom Werner wrote:
> Logan Capaldo wrote:
> >
> > On Jul 21, 2006, at 1:53 PM, Theallnighter Theallnighter wrote:
> >
> >> Hi all,
> >> how can i delete all non alphanumeric characters in a string ? thanks
> >>
> >> --
> >> Posted via http://www.ruby-....
> >>
> >
> > string.gsub(/[0-9a-z]+/i, '')
> >
> >
> >
> That deletes all alphanumeric. To delete all non-alphanumeric:
>
> string.gsub(/[^0-9a-z]/i, '')
>
> --
> Tom Werner
> Helmets to Hardhats
> Software Developer
> tom@helmetstohardhats.org
> www.helmetstohardhats.org

Jim Cochrane

7/21/2006 8:15:00 PM

0

On 2006-07-21, Jim Cochrane <allergic-to-spam@no-spam-allowed.org> wrote:
> On 2006-07-21, Logan Capaldo <logancapaldo@gmail.com> wrote:
>>
>> On Jul 21, 2006, at 3:40 PM, Jim Cochrane wrote:
>>
>>> On 2006-07-21, Theallnighter Theallnighter
>>> <theallnighter@gmail.com> wrote:
>>>> Hi all,
>>>> how can i delete all non alphanumeric characters in a string ? thanks
>>>>
>>> ...
>>> #!/usr/bin/ruby
>>>
>>> x = "There are 2007 beans and 15234 grains of rice in this bag."
>>> puts x
>>> x.gsub!(/\W/, '')
>>> puts x
>>> ...
>>>
>>>
>>
>> Well the only "problem" with that is
>>
>> x = '\w includes_under_scores_too'
>>
>
> Woah! Thanks for pointing that out. It looks like
> http://www.ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/r...
> has a bug:
>
> \w letter or digit; same as [0-9A-Za-z]
>
> It's missing a _.
>
> Here's a fixed version:
>
>
> #!/usr/bin/ruby
>
> x = "There are 2007 beans_and 15234 grains of rice in this bag."
> puts x
> x.gsub!(/\W/, '')
> puts x
> x.gsub!(/\W|_/, '')
> puts "fixed:"
> puts x

Oops - the above has a bug (although it still "works"). Here's a fixed
version, with an opposite example further demonstrating the bug in the
ruby doc site:


#!/usr/bin/ruby

s = "There are 2007 beans_and 15234 grains of rice in this bag."
x = s.dup
y = s.dup
puts "original:"
puts x
x.gsub!(/\W/, '')
puts "\nbroken:"
puts x
y.gsub!(/\W|_/, '')
puts "\nfixed:"
puts y

puts "\nopposite:"
z = s.dup
z.gsub!(/\w/, '')
puts z

--

original:
There are 2007 beans_and 15234 grains of rice in this bag.

broken:
Thereare2007beans_and15234grainsofriceinthisbag

fixed:
Thereare2007beansand15234grainsofriceinthisbag

opposite:
.

Tom Werner

7/21/2006 8:19:00 PM

0

dominique.plante@gmail.com wrote:
> for fun, I started irb, then typed
>
> "567576hgjhgjh&**)".gsub(/^[0-9a-z]/i, '')
>
> It returned
>
> 67576hgjhgjh&**)
>
>

The carat goes inside the brackets (it inverses the character class)

Tom

--
Tom Werner
Helmets to Hardhats
Software Developer
tom@helmetstohardhats.org
www.helmetstohardhats.org