Reg Ex exclusion

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

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

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

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

On Sep 2, 9:17 am, Ne Scripter [email protected]
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

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

Josh C. wrote:

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

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

thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs