# RegExp problem

**URL:** <https://www.ruby-forum.com/t/regexp-problem/138116>\
**Category:** Rails\
**Created:** [May 20, 2008, 10:41am UTC](https://www.ruby-forum.com/t/regexp-problem/138116 "2008-05-20T10:41:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 20, 2008, 10:41am UTC](https://www.ruby-forum.com/t/regexp-problem/138116/1 "2008-05-20T10:41:58Z")

</div>

Hi,

I’ve got a string “foo(n\_characters)”. ex: foo(bar) .

I would like to extract bar.

What RegExp can do that?

thanks

---

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 20, 2008, 11:16am UTC](https://www.ruby-forum.com/t/regexp-problem/138116/2 "2008-05-20T11:16:48Z")

</div>

jef wrote:

> 

Something like this?

> > s = ‘foo(bar)’  
> > =\> “foo(bar)”
> 
> > myarray = s.scan(/foo((\w+))/)  
> > =\> [[“bar”]]

---

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 20, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/regexp-problem/138116/3 "2008-05-20T17:13:23Z")

</div>

Rory,  
I saw this thread and thought you might be able to point me in a  
RegExp direction to solve this problem.  
I’m importing .csv data from users’ databases and sometimes it’s UGLY  
with commas, pound signs, etc.  
I’d like to pass the string fields through some type of ‘cleansing’  
code that allows them to write to a MySQL string database field  
without COUGHING.  
I am grateful for any links or suggestions.  
Thank you,  
Kathleen

---

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 20, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/regexp-problem/138116/4 "2008-05-20T17:16:56Z")

</div>

Hi everyone,

I recently found Rubular [[http://www.rubular.com/](http://www.rubular.com/) ]. It could be very  
helpful for playing with regexes for cleaning up that data.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 20, 2008, 6:58pm UTC](https://www.ruby-forum.com/t/regexp-problem/138116/5 "2008-05-20T18:58:08Z")

</div>

Craig D. wrote:

> Hi everyone,
> 
> I recently found Rubular [[http://www.rubular.com/](http://www.rubular.com/) ]. It could be very  
> helpful for playing with regexes for cleaning up that data.

Thanks for the tip Craig - looks sweet!

---

<div class="post-metadata">

**Author:** ![jef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@jef](https://www.ruby-forum.com/u/jef)\
**Post date:** [May 20, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/regexp-problem/138116/6 "2008-05-20T17:43:44Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Kathleen  
>   
> Hi Kathy

Regexps may not be the ideal solution, but it very much depends on what  
you mean by “cleansing”:

a) Keeping the data intact, and making sure it will write to the DB  
(i.e. escaping “problem” characters so that they don’t break the insert  
query)  
or  
b) Actively removing/replacing stuff from the original data (e.g.  
replacing all the pound signs with tildes).

From the little I know about ActiveRecord, Rails’ should pretty much  
handle (a) - but (b) would be a better fit for a regexp, depending on  
the degree of complexity (It’s always better to use something else other  
than regexps, if there is a sane alternative).

Can you give some more info, on how exactly you want to cleanse the  
data?
