# Gsub and regex

**URL:** <https://www.ruby-forum.com/t/gsub-and-regex/140777>\
**Category:** Ruby\
**Created:** [June 18, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/gsub-and-regex/140777 "2008-06-18T13:30:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [June 18, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/gsub-and-regex/140777/1 "2008-06-18T13:30:56Z")

</div>

I’m trying to replace all /\n / with /\n/ in a string, but gsub won’t  
allow a regex as the replacement when a match occurs. I’m getting myself  
considerably confused about data types and regex, and just plain need  
some help. Is there some way to do this replacement, using gsub?

Thanks,

t.

–

```auto
Tom C., MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [June 18, 2008, 1:33pm UTC](https://www.ruby-forum.com/t/gsub-and-regex/140777/2 "2008-06-18T13:33:18Z")

</div>

On Wednesday 18 June 2008, Tom C. wrote:

> I’m trying to replace all /\n / with /\n/ in a string, but gsub won’t  
> allow a regex as the replacement when a match occurs. I’m getting myself  
> considerably confused about data types and regex, and just plain need  
> some help. Is there some way to do this replacement, using gsub?
> 
> Thanks,
> 
> t.

I’m not sure I understand you. Do you want to replace all newline  
charactes  
followed by a space with a single newline character? If so, you don’t  
need  
regexps at all:

str.gsub("\n ", “\n”)

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [June 18, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/gsub-and-regex/140777/3 "2008-06-18T13:45:44Z")

</div>

Stefano C. wrote:

> >

Man, this list is absolutely amazing. Here I am programming at 4:30 in  
the morning, and get stuck, and before I can execute my program again, I  
get my problem solved. I think I want to live in Ruby land the rest of  
my life.

And…how could I have forgotten that in “…” \n get understood. Jeez.  
So easy.

Thanks for being there.

t.

–

```auto
Tom C., MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)

```
