Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Newbie Question: delete all non alphanumeric characters

Reply
Thread Tools

Newbie Question: delete all non alphanumeric characters

 
 
Theallnighter Theallnighter
Guest
Posts: n/a
 
      07-21-2006
Hi all,
how can i delete all non alphanumeric characters in a string ? thanks

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

 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      07-21-2006

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-forum.com/.
>


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


 
Reply With Quote
 
 
 
 
Tom Werner
Guest
Posts: n/a
 
      07-21-2006
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-forum.com/.
>>

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

www.helmetstohardhats.org


 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      07-21-2006

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-forum.com/.
>>>

>>
>> 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
>
> www.helmetstohardhats.org
>
>


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


 
Reply With Quote
 
Jim Cochrane
Guest
Posts: n/a
 
      07-21-2006
On 2006-07-21, Theallnighter Theallnighter <> 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

--

 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      07-21-2006

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

> On 2006-07-21, Theallnighter Theallnighter
> <> 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'


 
Reply With Quote
 
Jim Cochrane
Guest
Posts: n/a
 
      07-21-2006
On 2006-07-21, Logan Capaldo <> wrote:
>
> On Jul 21, 2006, at 3:40 PM, Jim Cochrane wrote:
>
>> On 2006-07-21, Theallnighter Theallnighter
>> <> 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-do...rg/regexp.html
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
 
Reply With Quote
 
dominique.plante@gmail.com
Guest
Posts: n/a
 
      07-21-2006
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-forum.com/.
> >>

> >
> > 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
>
> www.helmetstohardhats.org


 
Reply With Quote
 
Jim Cochrane
Guest
Posts: n/a
 
      07-21-2006
On 2006-07-21, Jim Cochrane <allergic-to-> wrote:
> On 2006-07-21, Logan Capaldo <> wrote:
>>
>> On Jul 21, 2006, at 3:40 PM, Jim Cochrane wrote:
>>
>>> On 2006-07-21, Theallnighter Theallnighter
>>> <> 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-do...rg/regexp.html
> 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:
 
Reply With Quote
 
Tom Werner
Guest
Posts: n/a
 
      07-21-2006
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

www.helmetstohardhats.org


 
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
re.match and non-alphanumeric characters The Web President Python 8 11-17-2008 12:02 AM
Password length minimum: 7. Non-alphanumeric characters required. Yasin Cepeci ASP .Net Security 2 04-26-2007 10:11 PM
Password length minimum: 7. Non-alphanumeric characters required: 1. Yasin Cepeci ASP .Net 1 04-26-2007 05:09 PM
remove non alphanumeric characters joe C Programming 5 03-05-2007 01:38 PM
Easy way to specify all non-alphanumeric characters? Steven J Sobol Java 8 04-30-2004 09:15 AM



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