# How to replace a string with given pattern?

**URL:** <https://www.ruby-forum.com/t/how-to-replace-a-string-with-given-pattern/147388>\
**Category:** Ruby\
**Created:** [September 1, 2008, 10:41am UTC](https://www.ruby-forum.com/t/how-to-replace-a-string-with-given-pattern/147388 "2008-09-01T10:41:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 1, 2008, 10:41am UTC](https://www.ruby-forum.com/t/how-to-replace-a-string-with-given-pattern/147388/1 "2008-09-01T10:41:48Z")

</div>

I want to replace a string with the new value on a given regular  
pattern. For example:

“Hello”.replace("/H/", “h”) #=\> hello

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 1, 2008, 10:49am UTC](https://www.ruby-forum.com/t/how-to-replace-a-string-with-given-pattern/147388/2 "2008-09-01T10:49:12Z")

</div>

2008/9/1 Zhao Yi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I want to replace a string with the new value on a given regular  
> pattern. For example:
> 
> “Hello”.replace(“/H/”, “h”) #=\> hello

Please look it up in the docs

[http://www.ruby-doc.org/](http://www.ruby-doc.org/)

Cheers

robert

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 1, 2008, 10:52am UTC](https://www.ruby-forum.com/t/how-to-replace-a-string-with-given-pattern/147388/3 "2008-09-01T10:52:09Z")

</div>

On Mon, Sep 1, 2008 at 10:36 AM, Zhao Yi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to replace a string with the new value on a given regular  
> pattern. For example:
> 
> “Hello”.replace(“/H/”, “h”) #=\> hello

You can use String#sub[1] to replace one occurance:  
“HHello”.sub(/H/, “h”) #=\> “hHello”

or gsub[2] to replace all:  
“HHello”.gsub(/H/, “h”)

[1] [gsub (String) - APIdock](http://apidock.com/ruby/String/gsub)  
[2] [gsub (String) - APIdock](http://apidock.com/ruby/String/gsub)

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 1, 2008, 11:03am UTC](https://www.ruby-forum.com/t/how-to-replace-a-string-with-given-pattern/147388/4 "2008-09-01T11:03:46Z")

</div>

Thomas W. wrote:

> On Mon, Sep 1, 2008 at 10:36 AM, Zhao Yi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I want to replace a string with the new value on a given regular  
> > pattern. For example:
> > 
> > “Hello”.replace(“/H/”, “h”) #=\> hello
> 
> You can use String#sub[1] to replace one occurance:  
> “HHello”.sub(/H/, “h”) #=\> “hHello”
> 
> or gsub[2] to replace all:  
> “HHello”.gsub(/H/, “h”)
> 
> [1] [gsub (String) - APIdock](http://apidock.com/ruby/String/gsub)  
> [2] [gsub (String) - APIdock](http://apidock.com/ruby/String/gsub)

ok thanks.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 1, 2008, 10:55am UTC](https://www.ruby-forum.com/t/how-to-replace-a-string-with-given-pattern/147388/5 "2008-09-01T10:55:41Z")

</div>

Zhao Yi wrote:

> I want to replace a string with the new value on a given regular  
> pattern. For example:
> 
> “Hello”.replace("/H/", “h”) Â #=\> hello

String#gsub (which is in the docs, btw)

HTH,  
Sebastian
