# Keep only one part of a string

**URL:** <https://www.ruby-forum.com/t/keep-only-one-part-of-a-string/68743>\
**Category:** Ruby\
**Created:** [September 15, 2006, 1:26am UTC](https://www.ruby-forum.com/t/keep-only-one-part-of-a-string/68743 "2006-09-15T01:26:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 15, 2006, 1:26am UTC](https://www.ruby-forum.com/t/keep-only-one-part-of-a-string/68743/1 "2006-09-15T01:26:49Z")

</div>

Hello, how to better write that : self.ident = self.ident[/0-9\*/]

I’m pretty sure there is a String method that would keep only the first  
match of the regex but I didn’t found it in the String doc reference

Thanks

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 15, 2006, 1:42am UTC](https://www.ruby-forum.com/t/keep-only-one-part-of-a-string/68743/2 "2006-09-15T01:42:21Z")

</div>

Zouplaz wrote:

> Hello, how to better write that : self.ident = self.ident[/0-9\*/]
> 
> I’m pretty sure there is a String method that would keep only the first  
> match of the regex but I didn’t found it in the String doc reference

ri String#slice, ri String#slice!

> Thanks

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 15, 2006, 11:35am UTC](https://www.ruby-forum.com/t/keep-only-one-part-of-a-string/68743/3 "2006-09-15T11:35:32Z")

</div>

le 15/09/2006 01:43, Eero S. nous a dit:

> 

Fine ! Thanks… About the same topic (Strings) I wonder how to replace  
“REF:90 REFERAL”.gsub!(‘REF:’,’’)  
by something else

I tried with .delete but

“REF:90 REFERAL”.delete!(“REF:”) delete every R, E or F char in the  
string

How to use delete ?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 15, 2006, 12:05pm UTC](https://www.ruby-forum.com/t/keep-only-one-part-of-a-string/68743/4 "2006-09-15T12:05:34Z")

</div>

Zouplaz wrote:

> > 
> 
> Fine ! Thanks… About the same topic (Strings) I wonder how to replace  
> “REF:90 REFERAL”.gsub!(‘REF:’,’’)  
> by something else

What something else? Please say specifically what you want to  
accomplish.  
Post an example string before you apply your desired remedy, and after.

> I tried with .delete but
> 
> “REF:90 REFERAL”.delete!(“REF:”) delete every R, E or F char in the string
> 
> How to use delete ?

You are asking how to apply a particular solution, but without first  
stating  
a problem. Maybe the problem is better solved using a different method.

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 15, 2006, 12:11pm UTC](https://www.ruby-forum.com/t/keep-only-one-part-of-a-string/68743/5 "2006-09-15T12:11:35Z")

</div>

le 15/09/2006 12:01, Paul L. nous a dit:

> > > > Thanks
> > 
> > Fine ! Thanks… About the same topic (Strings) I wonder how to replace  
> > “REF:90 REFERAL”.gsub!(‘REF:’,’’)  
> > by something else
> 
> What something else? Please say specifically what you want to accomplish.  
> Post an example string before you apply your desired remedy, and after.

Sorry, what I would like to know is if there is a another String method  
to delete a part of a string instead of gsub

In the string “REF:90 REFERAL”

I want REF: to be deleted but not the REF or REFERAL

I’ve tried with the .delete method but each R, E F : char was deleted in  
the string

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 15, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/keep-only-one-part-of-a-string/68743/6 "2006-09-15T15:00:15Z")

</div>

On Fri, Sep 15, 2006 at 07:10:52PM +0900, Zouplaz wrote:

> > > >
> 
> Sorry, what I would like to know is if there is a another String method  
> to delete a part of a string instead of gsub
> 
> In the string “REF:90 REFERAL”
> 
> I want REF: to be deleted but not the REF or REFERAL
> 
> I’ve tried with the .delete method but each R, E F : char was deleted in  
> the string  
> s = “REF:90 REFERAL”  
> s.slice!(“REF:”)  
> s #=\> “90 REFERAL”

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 15, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/keep-only-one-part-of-a-string/68743/7 "2006-09-15T13:41:57Z")

</div>

Zouplaz wrote:

> Sorry, what I would like to know is if there is a another String method  
> to delete a part of a string instead of gsub
> 
> In the string “REF:90 REFERAL”
> 
> I want REF: to be deleted but not the REF or REFERAL
> 
> I’ve tried with the .delete method but each R, E F : char was deleted in  
> the string

You want String#slice!

s = ‘REF:90 REFERAL’  
s.slice!(‘REF:’) # =\> “REF:”  
s # =\> “90 REFERAL”

Check out the core documentation for the String class:  
[http://ruby-doc.org/core/classes/String.html](http://ruby-doc.org/core/classes/String.html)

Regards,  
Jordan
