# Reg Ex exclusion

**URL:** <https://www.ruby-forum.com/t/reg-ex-exclusion/174816>\
**Category:** Ruby\
**Created:** [September 2, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/reg-ex-exclusion/174816 "2009-09-02T18:17:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 2, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/reg-ex-exclusion/174816/1 "2009-09-02T18:17:52Z")

</div>

Hi all,

I thought this would be easy, but I am clearly mixing up my logic. I  
have a string like so

string = “joe|blogg|clothing|”

I want to remove the last pipe with a reg ex and I am trying to use the  
following code to do so:

string(/|$/, 1)

This however is not doing anything except delete the entire string, any  
suggestion are greatly appreciated.

Many thanks

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 2, 2009, 6:47pm UTC](https://www.ruby-forum.com/t/reg-ex-exclusion/174816/2 "2009-09-02T18:47:26Z")

</div>

Use sub or sub! “joe|blogg|clothing|”.sub(/|$/,’’)

On Wed, Sep 2, 2009 at 11:17 AM, Ne Scripter \<

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 3, 2009, 1:46am UTC](https://www.ruby-forum.com/t/reg-ex-exclusion/174816/3 "2009-09-03T01:46:05Z")

</div>

> > string = “joe|blogg|clothing|”  
> > =\> “joe|blogg|clothing|”
> 
> > string.chop  
> > =\> “joe|blogg|clothing”
> 
> > string[/(.\*)|$/,1]  
> > =\> “joe|blogg|clothing”
> 
> > string.split("|").join("|")  
> > =\> “joe|blogg|clothing”

The regex solution submitted before is good, though if all of your  
cases are this straight forward, I think string.chop is hard to beat.  
-tim

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 2, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/reg-ex-exclusion/174816/4 "2009-09-02T18:57:59Z")

</div>

On Sep 2, 9:17 am, Ne Scripter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> string(/|$/, 1)
> 
> This however is not doing anything except delete the entire string, any  
> suggestion are greatly appreciated.
> 
> ## Many thanks
> 
> Posted viahttp://www.ruby-forum.com/.

Are you actually getting results from “string(/|$/,1)”? I get what I  
would expect:  
irb(main):001:0\> st=“joe|blogg|clothing|”  
=\> “joe|blogg|clothing|”  
irb(main):004:0\> st(/(.\*)|$/)  
#Class:0x3f99248: undefined method `st’ for main:Object

I think you mean []

Try this:

string[/(.\*)|$/,1]

irb(main):010:0\> st[/(.\*)|$/,1]  
=\> “joe|blogg|clothing”

By the way, if you’re doing this to split up that string into joe,  
blogg and clothing later, you can just skip the Regex and use split:

irb(main):011:0\> st.split(“|”)  
=\> [“joe”, “blogg”, “clothing”]

-Dylan

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 3, 2009, 9:37am UTC](https://www.ruby-forum.com/t/reg-ex-exclusion/174816/5 "2009-09-03T09:37:43Z")

</div>

Thanks all for your help. I went for the regular expression in the end  
for consistency with other code.

Thanks a lot

Tlb Fpx wrote:

> Josh C. wrote:
> 
> > Use sub or sub! “joe|blogg|clothing|”.sub(/|$/,’’)
> > 
> > On Wed, Sep 2, 2009 at 11:17 AM, Ne Scripter \<
> 
> thanks

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 3, 2009, 6:17am UTC](https://www.ruby-forum.com/t/reg-ex-exclusion/174816/6 "2009-09-03T06:17:32Z")

</div>

Josh C. wrote:

> Use sub or sub! “joe|blogg|clothing|”.sub(/|$/,’’)
> 
> On Wed, Sep 2, 2009 at 11:17 AM, Ne Scripter \<

thanks
