# Newbie regular expression question

**URL:** <https://www.ruby-forum.com/t/newbie-regular-expression-question/186448>\
**Category:** Rails\
**Created:** [April 7, 2010, 11:44am UTC](https://www.ruby-forum.com/t/newbie-regular-expression-question/186448 "2010-04-07T11:44:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 7, 2010, 11:44am UTC](https://www.ruby-forum.com/t/newbie-regular-expression-question/186448/1 "2010-04-07T11:44:49Z")

</div>

Hello champs,

I am having an string and i want all extra characters to be removed from  
that string except + and .  
How to write a regular expression for it… Currently i am trying it like  
this as word.sub(/[\_-:\/]/, “”)… But its not working…

I can do it like this as well:- word.sub(/[\W]/, “”) but this will  
replace all characters even also + and .

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 7, 2010, 11:51am UTC](https://www.ruby-forum.com/t/newbie-regular-expression-question/186448/2 "2010-04-07T11:51:38Z")

</div>

Give sample input string and output string…

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 7, 2010, 11:58am UTC](https://www.ruby-forum.com/t/newbie-regular-expression-question/186448/3 "2010-04-07T11:58:18Z")

</div>

Le 07/04/2010 11:44, Hemant B. a écrit :

> Hello champs,
> 
> I am having an string and i want all extra characters to be removed from  
> that string except + and .  
> How to write a regular expression for it… Currently i am trying it like  
> this as word.sub(/[\_-:\/]/, “”)… But its not working…
> 
> I can do it like this as well:- word.sub(/[\W]/, “”) but this will  
> replace all characters even also + and .

This seems to be what you need :

str = “ABCD EF + (GH\_IJ).klm \_ ^^z”  
str.gsub(/[^\w+.]|\_/i, ‘’)  
=\> “ABCDEF+GHIJ.klmz”

–  
Aurélien AMILIN

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 7, 2010, 12:00pm UTC](https://www.ruby-forum.com/t/newbie-regular-expression-question/186448/4 "2010-04-07T12:00:39Z")

</div>

On 7 April 2010 10:44, Hemant B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am having an string and i want all extra characters to be removed from  
> that string except + and .

word.gsub(/[^+.]/, “”)

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 7, 2010, 11:55am UTC](https://www.ruby-forum.com/t/newbie-regular-expression-question/186448/5 "2010-04-07T11:55:22Z")

</div>

Siddick E. wrote:

> Give sample input string and output string…

Hey thanks for such a quick reply…

Ok lets suppose,  
Input:- :removed\_email\_address@domain.invalid  
Output should be as only, removed\_email\_address@domain.invalid

Means that i want to remove all extra chars except . and +… Hope u got  
it now…

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 7, 2010, 12:02pm UTC](https://www.ruby-forum.com/t/newbie-regular-expression-question/186448/6 "2010-04-07T12:02:59Z")

</div>

The question or the example isn’t clear enough.

But for the example you gave, this works = a.sub(/[\s\_]/,’’)

Thanks & Regards,  
Dhruva S…

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 7, 2010, 12:02pm UTC](https://www.ruby-forum.com/t/newbie-regular-expression-question/186448/7 "2010-04-07T12:02:24Z")

</div>

On 7 April 2010 10:58, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 7 April 2010 10:44, Hemant B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am having an string and i want all extra characters to be removed from  
> > that string except + and .
> 
> word.gsub(/[^+.]/, “”)

oh… if you want to keep alphanumeric characters:

word.gsub(/[^a-zA-Z0-9+.]/, “”)

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [April 7, 2010, 12:09pm UTC](https://www.ruby-forum.com/t/newbie-regular-expression-question/186448/8 "2010-04-07T12:09:44Z")

</div>

AMILIN AurÃ©lien wrote:

> Le 07/04/2010 11:44, Hemant B. a ï¿½crit :
> 
> > Hello champs,
> > 
> > I am having an string and i want all extra characters to be removed from  
> > that string except + and .  
> > How to write a regular expression for it… Currently i am trying it like  
> > this as word.sub(/[\_-:\/]/, “”)… But its not working…
> > 
> > I can do it like this as well:- word.sub(/[\W]/, “”) but this will  
> > replace all characters even also + and .
> 
> This seems to be what you need :
> 
> str = “ABCD EF + (GH\_IJ).klm \_ ^^z”  
> str.gsub(/[^\w+.]|\_/i, ‘’)  
> =\> “ABCDEF+GHIJ.klmz”
> 
> –  
> Aurï¿½lien AMILIN

Thanks guyz, this worked like a charm… Thanks to all of you who replies  
so quickly …
